使用文件提供程序实现 UIDocumentPickerModeOpen
Posted
技术标签:
【中文标题】使用文件提供程序实现 UIDocumentPickerModeOpen【英文标题】:Implementing UIDocumentPickerModeOpen with File Providers 【发布时间】:2015-03-24 20:57:04 【问题描述】:是否有人为文件提供程序应用扩展成功实现了“打开”操作?当用户最初在文档选择器扩展中选择文件时,我已经能够读取文件(本质上,这是“导入”操作)。但除此之外的任何事情都失败了。以下是我遇到的问题:
如果我使用NSFileCoordinator
,应用程序会死锁。
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。谢谢, 【参考方案1】:最后,我认为我已经通过在各处删除文件协调并忽略安全范围书签常量来使其正常工作。这是我在文件提供程序扩展中用于startProvidingItemAtURL:
的内容:
- (void)startProvidingItemAtURL:(NSURL *)url completionHandler:(void (^)(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 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];
【讨论】:
你能帮忙***.com/questions/30613645/…【参考方案2】:您不应该调用文件协调器调用:staringProvidingItemsAtUrl
检查苹果 cmets 的方法,它说:- (void)startProvidingItemAtURL:(NSURL *)url completionHandler:(void (^)(NSError *error))completionHandler
Note
Do not use file coordination inside this method. The system already guarantees that no other process can access the file while this method is executing.
删除后:[self.fileCoordinator coordinateWritingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL)
僵局应该会消失。
还:
NSURLBookmarkCreationWithSecurityScope
不适用于 IOS 且 IOS 没有此选项。 IOS 不需要此选项。苹果文档对此非常困惑。
【讨论】:
以上是关于使用文件提供程序实现 UIDocumentPickerModeOpen的主要内容,如果未能解决你的问题,请参考以下文章
实现 Dijkstra 的 Java 程序不读取 txt 文件