我的应用程序支持就地打开文档。但是,在特定条件下,我希望将其复制到本地沙箱。我遵循Copy file from iOS 11 Files app to sandbox并使用[NSURL startsourcingSecurityScopedResource]。
我验证了文件存在,但复制操作失败,报告文件不存在。
下面是我在app delegate中的代码。
if (openedInPlace) {
NSString *urlString = [url absoluteString];
if ([urlString containsString:@"/Downloads/"]) {
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *coordinatorError = nil;
__block BOOL leaveInPlace = YES;
__block NSURL *fileURL;
[fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:&coordinatorError byAccessor:^(NSURL *newURL)
{
// Copy to local inbox
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *docs = [fm URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *inbox = [docs URLByAppendingPathComponent:@"Inbox" isDirectory:YES];
//NSURL *fileURL = [inbox URLByAppendingPathComponent:[url lastPathComponent]];
fileURL = [inbox URLByAppendingPathComponent:@"test_doc.ptw"];
BOOL result;
NSLog(@"Copy from %@ to %@", newURL, fileURL);
result = [newURL startAccessingSecurityScopedResource];
NSLog(@"startAccessingSecurityScopedResource %@", result ? @"succeeded" : @"did not succeed.");
result = [fm fileExistsAtPath:newURL.path];
NSLog(@"File %@ %@", newURL.path, result ? @"exists" : @"doesn't exist");
NSError *copyError = nil;
[fm copyItemAtURL:newURL toURL:fileURL error:©Error];
if (!copyError)
{
// OK
leaveInPlace = NO;
}
else
{
NSLog(@"Files app error: %@", copyError);
leaveInPlace = YES;
result = [fm fileExistsAtPath:newURL.path];
NSLog(@"File %@ %@", newURL.path, result ? @"exists" : @"doesn't exist");
}
[newURL stopAccessingSecurityScopedResource];
}];
openedInPlace = leaveInPlace;
if (! openedInPlace)
url = fileURL;
}
}
字符串
这里是日志:
Copy from file:///private/var/mobile/Containers/Shared/AppGroup/E6E86AC8-4E2F-45B2-9842-FB6AAE29D31E/File%20Provider%20Storage/Downloads/Bloc%202.ptw to file:///var/mobile/Containers/Data/Application/912852F7-9C16-4D9C-B9C9-6A0126C92C40/Documents/Inbox/test_doc.ptw
startAccessingSecurityScopedResource succeeded
File /private/var/mobile/Containers/Shared/AppGroup/E6E86AC8-4E2F-45B2-9842-FB6AAE29D31E/File Provider Storage/Downloads/Bloc 2.ptw exists
Files app error: Error Domain=NSCocoaErrorDomain Code=4 "The file “Bloc 2.ptw” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Shared/AppGroup/E6E86AC8-4E2F-45B2-9842-FB6AAE29D31E/File Provider Storage/Downloads/Bloc 2.ptw, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/var/mobile/Containers/Data/Application/912852F7-9C16-4D9C-B9C9-6A0126C92C40/Documents/Inbox/test_doc.ptw, NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/E6E86AC8-4E2F-45B2-9842-FB6AAE29D31E/File Provider Storage/Downloads/Bloc 2.ptw, NSUnderlyingError=0x28252c570 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
File /private/var/mobile/Containers/Shared/AppGroup/E6E86AC8-4E2F-45B2-9842-FB6AAE29D31E/File Provider Storage/Downloads/Bloc 2.ptw exists
型
我尝试在fileCoordinator块中使用“url”而不是“newURL”。
1条答案
按热度按时间monwx1rj1#
错误消息具有误导性,问题出在复制目标上,而不是消息所指示的源。目标文件夹在我的应用程序中不存在。我现在将文件“test_doc.ptw”复制到Documents目录,一切正常。