写入URL时出现NSFileWrapper错误

时间:2013-01-20 04:50:06

标签: ios nsfilewrapper

我有一个档案我正在尝试将内容复制到Documents目录中。它在模拟器中工作正常,但在设备上出错。

- (BOOL)importData:(NSData *)zippedData {

// Read data into a dir Wrapper

NSData *unzippedData = [zippedData gzipInflate];

NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc] initWithSerializedRepresentation:unzippedData] autorelease];

if (dirWrapper == nil) {

    NSLog(@"Error creating dir wrapper from unzipped data");

    return FALSE;

}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSURL *dirUrl = [NSURL fileURLWithPath:documentsDirectory];  

NSError *error;

BOOL success = [dirWrapper writeToURL:dirUrl options:NSFileWrapperWritingAtomic originalContentsURL:nil error:&error];

if (!success) {

    NSLog(@"Error importing file: %@", error.localizedDescription);

    return FALSE;

}

// Success!

return TRUE;    

}

我在控制台中获得以下输出:

2013-01-19 22:40:23.865 My App[569:907] Foundation called mkdir("/var/mobile/Applications/1BF53E96-6261-4DEC-8700-60307D84587F/(A Document Being Saved By My App)"), it didn't return 0, and errno was set to 1.

2013-01-19 22:40:23.922 My App[569:907] Error importing file: The operation couldn’t be completed. (Cocoa error 513.)

1 个答案:

答案 0 :(得分:0)

我认为你只能写到手机上的Documents目录,因此在你的文档(A Document Being ...)之前将“Documents”添加到你的dirurl中