Zeb*_*Zeb 5 nsfilemanager ios ios10 xcode8
我正在将应用程序从XCode7和iOS 9.x移植到XCode8和iOS10。我正在努力处理文件。
我需要从后端下载文件,然后将其从/Documents移到/tmp。这是我的代码:
AFURLSessionManager *manager = ...
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if(error) {
...
} else {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpPDFPath = [tmpDirectory stringByAppendingPathComponent:[[response suggestedFilename] stringByReplacingOccurrencesOfString:@" " withString:@""]];
if ([fileManager fileExistsAtPath:tmpPDFPath] == YES) {
[fileManager removeItemAtPath:tmpPDFPath error:&error];
}
NSLog(@"readable %d", [fileManager isReadableFileAtPath:filePath]);
// Print TRUE
NSLog(@"tmpWritable %d", [fileManager isWritableFileAtPath:[NSURL URLWithString:tmpDirectory]]);
// Print TRUE
BOOL move = [fileManager moveItemAtPath:filePath toPath:tmpPDFPath error:&error];
...
}
}];
Run Code Online (Sandbox Code Playgroud)
如果我在iOS 9.3模拟器中运行应用程序,则一切正常,但在iOS10模拟器中运行时,应用程序崩溃。我要做的第一个更改是传递给moveItemAtPath方法filePath.absoluteString而不是filePath。尽管进行了此编辑,但是move方法始终因以下错误而失败:
错误域= NSCocoaErrorDomain代码= 4“” XXXX.pdf“ 不能移动到” tmp“,因为前者不存在或包含后者的文件夹不存在。 ” UserInfo = {NSSourceFilePathErrorKey = / file :/Users/XXX/Library/Developer/CoreSimulator/Devices/24CAB2B2-F495-4CFF-90A7-5C51AF38C194/data/Containers/Data/Application/3D8EEEF9-F639-4D6C-BD5E-17A571F7B836/Documents/XXXX.pdf,NSUserStringVariant = ( 移动 ),NSFilePath = / file:/ Users / XXXX / Library / Developer / CoreSimulator / Devices / 24CAB2B2-F495-4CFF-90A7-5C51AF38C194 / data / Containers / Data / Application / 3D8EEEF9-F639-4D6C-BD5E-17A571F7B836 / Documents / “ XXXX.pdf,NSDestinationFilePath = /用户/” XXXX / Library / Developer / CoreSimulator / Devices / 24CAB2B2-F495-4CFF-90A7-5C51AF38C194 / data / Containers / Data / Application / 3D8EEEF9-F639-4D6C-BD5E-17A571F7B836 / tmp /“XXXX.pdf,NSUnderlyingError=0x7b0a3500 { 错误域= NSPOSIXErrorDomain代码= 2“没有这样的文件或目录” }}
有没有人已经处理过这种错误?
我的第一个解决方法是通过NSData:
NSError* errorWrite = nil;
NSData* data = [NSData dataWithContentsOfURL:filePath];
BOOL write = [data writeToFile:tmpPDFPath options:NSDataWritingAtomic error:&errorWrite];
Run Code Online (Sandbox Code Playgroud)
这段代码可以正常工作,但是我想理解为什么上一个代码不行。
| 归档时间: |
|
| 查看次数: |
2115 次 |
| 最近记录: |