【发布时间】:2015-03-24 20:57:04
【问题描述】:
是否有人为文件提供程序应用扩展成功实现了“打开”操作?当用户最初在文档选择器扩展中选择文件时,我已经能够读取文件(本质上,这是“导入”操作)。但除此之外的任何事情都失败了。以下是我遇到的问题:
- 如果我使用
NSFileCoordinator,应用程序会死锁。 -
如果我保存 URL 并稍后尝试读取或写入它,对如果我使用书签,这将有效。startAccessingSecurityScopedResource的调用将返回NO。 -
如果我尝试如果我在安全范围内创建书签,这将有效。bookmarkDataWithOptions:,我会返回Error Domain=NSCocoaErrorDomain Code=260 "The operation could not be completed. (Cocoa error 260.)"。
这是在创建文件提供程序扩展时为startProvidingItemAtURL: 创建的模板:
- (void)startProvidingItemAtURL:(NSURL *)url completionHandler:(void (^)(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: 的文档说 "注意
不要在这个方法中使用文件协调。” 所以我把它拿出来了。
在另一个应用程序中,这是我第一次读取该文件并为其创建书签的操作:
// 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 bookmarkDataWithOptions:options
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
options:options
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 值还是不使用它们?
【问题讨论】:
-
请添加代码以便我为您提供帮助。文件提供者有点复杂。甚至我们通过跟踪和错误方法对其进行跟踪
-
感谢您的回复。最后,我认为我通过删除文件协调器并忽略安全范围书签常量来使其正常工作。
-
你好,能否在github上分享一个简单的demo。谢谢,
标签: ios objective-c ios-app-extension