是否有人为文件提供程序应用扩展成功实现了“打开”操作?当用户最初在文档选择器扩展中选择文件时,我已经能够读取文件(本质上,这是“导入”操作)。但除此之外的任何事情都失败了。以下是我遇到的问题:
- 如果我使用
NSFileCoordinator ,应用程序会死锁。
如果我保存 URL 并稍后尝试读取或写入它,对 startAccessingSecurityScopedResource 的调用将返回 NO 。 如果我使用书签,这将有效。
如果我尝试 bookmarkDataWithOptions: ,我会返回 Error Domain=NSCocoaErrorDomain Code=260 "The operation could not be completed. (Cocoa error 260.)". 如果我在安全范围内创建书签,这将有效。
这是在创建文件提供程序扩展时为 startProvidingItemAtURL: 创建的模板:
- (void)startProvidingItemAtURLNSURL *)url completionHandlervoid (^)(NSError *))completionHandler {
// Should ensure that the actual file is in the position returned by URLForItemWithIdentifier:, then call the completion handler
NSError* error = nil;
__block NSError* fileError = nil;
NSData * fileData = [NSData data];
// TODO: get the contents of file at <url> from model
[self.fileCoordinator coordinateWritingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
[fileData writeToURL:newURL options:0 error:&fileError];
}];
if (error!=nil) {
completionHandler(error);
} else {
completionHandler(fileError);
}
}
但是当我使用文件协调器时,扩展会死锁。此外,startProvidingItemAtURL: 的文档说 "Note
不要在这个方法中使用文件协调。” 所以我把它拿出来了。
在另一个应用程序中,这是我第一次读取该文件然后为其创建书签的操作:
// Start accessing the security scoped resource.
[url startAccessingSecurityScopedResource];
void (^accessor)(NSURL *) = ^void(NSURL *url) {
// If the file is missing, create a default here. This really should be done inside
// the FileProvider method startProvidingItemAtURL:. Unfortunately, that method does
// not get called unless we use use the file coordinator, which can deadlock the app.
if (![url checkResourceIsReachableAndReturnError:nil]) {
// TODO: Create a real default file here.
[[NSFileManager defaultManager] createFileAtPath:url.path
contents:nil
attributes:nil];
}
// TODO: Do something with this file.
};
#ifdef USE_FILE_COORDINATOR
NSFileCoordinator *fileCoordinator = [NSFileCoordinator new];
[fileCoordinator coordinateReadingItemAtURL:url
options:NSFileCoordinatorReadingWithoutChanges
error:NULL
byAccessor:accessor];
#else
accessor(url);
#endif
// Store a bookmark for the url in the defaults so we can use it later.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSError *error = nil;
NSURLBookmarkCreationOptions options = 0;
#ifdef NSURLBookmarkCreationWithSecurityScope
options |= NSURLBookmarkCreationWithSecurityScope;
#endif
NSData *bookmarkData = [url bookmarkDataWithOptionsptions
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
if (error) {
NSLog(@"ERROR: %@", error);
}
[defaults setObject:bookmarkData forKey"BookmarkDataKey"];
// Stop accessing the security scoped resource.
[url stopAccessingSecurityScopedResource];
最后,为了稍后使用书签,我正在执行以下操作:
// Get the bookmark from the defaults file.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *bookmarkData = [defaults objectForKey"BookmarkDataKey"];
if (bookmarkData) {
// Convert the bookmark into a URL.
NSError *error;
BOOL bookmarkIsStale;
NSURLBookmarkResolutionOptions options = NSURLBookmarkResolutionWithoutUI;
#ifdef NSURLBookmarkResolutionWithSecurityScope
options |= NSURLBookmarkResolutionWithSecurityScope;
#endif
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmarkData
optionsptions
relativeToURL:nil
bookmarkDataIsStale:&bookmarkIsStale
error:&error];
// Get the data from the URL.
BOOL securitySucceeded = [url startAccessingSecurityScopedResource];
if (securitySucceeded) {
NSString *message = [NSString stringWithFormat"Random number: #%d", arc4random() % 10000];
NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:message];
NSError *fileError = nil;
[fileData writeToURL:url options:0 error:&fileError];
[url stopAccessingSecurityScopedResource];
}
}
如果我使用文件协调,第二个应用程序有时也会死锁。那么我是否也应该不在第二个应用程序中使用文件协调?问题是,如果我不使用文件协调,那么文件提供程序扩展中的 startProvidingItemAtURL: 似乎永远不会被调用。
另外,the documentation says使用 NSURLBookmarkCreationWithSecurityScope 但这对于 iOS 是未定义的。 NSURLBookmarkResolutionWithSecurityScope 也是如此。我应该只使用 OS X 值还是不使用它们?
Best Answer-推荐答案 strong>
最后,我认为我已经通过在各处删除文件协调并忽略安全范围书签常量来使其正常工作。这是我在文件提供程序扩展中用于 startProvidingItemAtURL: 的内容:
- (void)startProvidingItemAtURLNSURL *)url completionHandlervoid (^)(NSError *))completionHandler {
// If the file doesn't exist then create one.
if (![url checkResourceIsReachableAndReturnError:nil]) {
__block NSError *fileError = nil;
NSString *message = @"This is a test message";
NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:message];
[fileData writeToURL:url options:0 error:&fileError];
completionHandler(fileError);
}
}
在另一个应用程序中,这是我第一次读取该文件然后为其创建书签的操作:
// Start accessing the security scoped resource.
[url startAccessingSecurityScopedResource];
// If the file is missing, create a default here. This really should be done inside
// the FileProvider method startProvidingItemAtURL:. Unfortunately, that method does
// not get called unless we use use the file coordinator, which can deadlock the app.
if (![url checkResourceIsReachableAndReturnError:nil]) {
// TODO: Create a real default file here.
[[NSFileManager defaultManager] createFileAtPath:url.path
contents:nil
attributes:nil];
// TODO: Do something with this file.
// Store a bookmark for the url in the defaults so we can use it later.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSError *error = nil;
NSURLBookmarkCreationOptions options = 0;
#ifdef NSURLBookmarkCreationWithSecurityScope
options |= NSURLBookmarkCreationWithSecurityScope;
#endif
NSData *bookmarkData = [url bookmarkDataWithOptionsptions
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
if (error) {
NSLog(@"ERROR: %@", error);
}
[defaults setObject:bookmarkData forKey"BookmarkDataKey"];
// Stop accessing the security scoped resource.
[url stopAccessingSecurityScopedResource];
最后,为了稍后使用书签,我正在执行以下操作:
// Get the bookmark from the defaults file.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *bookmarkData = [defaults objectForKey"BookmarkDataKey];
if (bookmarkData) {
// Convert the bookmark into a URL.
NSError *error;
BOOL bookmarkIsStale;
NSURLBookmarkResolutionOptions options = NSURLBookmarkResolutionWithoutUI;
#ifdef NSURLBookmarkResolutionWithSecurityScope
options |= NSURLBookmarkResolutionWithSecurityScope;
#endif
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmarkData
optionsptions
relativeToURL:nil
bookmarkDataIsStale:&bookmarkIsStale
error:&error];
// Get the data from the URL.
BOOL securitySucceeded = [url startAccessingSecurityScopedResource];
if (securitySucceeded) {
NSString *message = [NSString stringWithFormat"Random number: #%d", arc4random() % 10000];
NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:message];
NSError *fileError = nil;
[fileData writeToURL:url options:0 error:&fileError];
[url stopAccessingSecurityScopedResource];
}
}
关于ios - 使用文件提供程序实现 UIDocumentPickerModeOpen,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29242732/
|