无权查看从 iOS 文档提供者在打开操作时传回的文档
No permission to view document passed back from iOS Document Provider on Open Operation
我正在 iOS 9.2 中创建一个 file/document 提供程序,并且导入和导出操作运行良好。但是,Open操作给我带来了麻烦。
我的文档提供程序将示例文件写入其文件提供程序存储,特别是在这个位置:
/private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File Provider Storage/test.txt
我已经能够从文档提供程序中读回它并验证数据是正确的。但是,当我尝试回读传递给主机应用程序的 URL 时,出现权限错误:
Error Domain=NSCocoaErrorDomain Code=257 "The file “test.txt” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File Provider Storage/test.txt, NSUnderlyingError=0x15eda7700 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
我使用的是相当简单的读写代码,但如果相关,我会在此处包含它:
文件写入:(在我调用 dismissGrantingAccessToURL 之前在文档提供程序中调用:)
NSString *contents = @"this is a dynamically created text file";
NSLog(@"write to file = %@", fileName);
NSError *err;
[contents writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:&err];
文件读取:(在 documentPicker 的主机应用程序内部调用:didPickDocumentAtURL:)
NSString *readback = [[NSString alloc] initWithContentsOfFile:[url path]
usedEncoding:nil
error:&err];
我从 Apple 的示例代码开始,并通读了他们的文档,但无法解释失败的原因。
对于我的文件提供者,只有 init: 函数被调用。我不确定这是否正常,或者是否应该为 Open 操作调用其他方法。这是我的初始化代码:
- (instancetype)init {
self = [super init];
if (self) {
[self.fileCoordinator coordinateWritingItemAtURL:[self documentStorageURL] options:0 error:nil byAccessor:^(NSURL *newURL) {
// ensure the documentStorageURL actually exists
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtURL:newURL withIntermediateDirectories:YES attributes:nil error:&error];
}];
}
return self;
}
我已经验证了上面的错误是零,新的URL如下:
file:///private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File%20Provider%20Storage/
更新:我开始明白为什么主机应用程序无法读取文件了。我认为这是因为我没有托管应用程序中该应用程序组的权限。但是,我认为尝试添加此权限没有意义,因为主机应用程序应与支持适当扩展的任何文档提供程序一起使用。
我想我的文件提供程序未被调用的原因(除了它的 init:) 是因为检测到 'open' 返回的文件是本地文件,因此不需要复制。要传递本地文件,文档说您必须传递位于 documentStorageURL 内的 URL,但(根据定义?)它将映射到不是的应用程序组可由主机应用程序访问。
所以我不确定这将如何运作。如果有人能阐明我在这里应该做什么,我将不胜感激。
在对文档进行了更多挖掘之后,我终于找到了问题所在:在我尝试访问它之前,我需要在 URL 上调用 "startAccessingSecurityScopedResource"。
参考本文档"Accessing files outside your sandbox"的"requirements"部分:
接受的答案是正确的,这里只是一个即时代码:
guard filePath.startAccessingSecurityScopedResource(),
let data = try? Data(contentsOf: filePath) else { return }
filePath.stopAccessingSecurityScopedResource()
// Do your business
我正在 iOS 9.2 中创建一个 file/document 提供程序,并且导入和导出操作运行良好。但是,Open操作给我带来了麻烦。
我的文档提供程序将示例文件写入其文件提供程序存储,特别是在这个位置:
/private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File Provider Storage/test.txt
我已经能够从文档提供程序中读回它并验证数据是正确的。但是,当我尝试回读传递给主机应用程序的 URL 时,出现权限错误:
Error Domain=NSCocoaErrorDomain Code=257 "The file “test.txt” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File Provider Storage/test.txt, NSUnderlyingError=0x15eda7700 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
我使用的是相当简单的读写代码,但如果相关,我会在此处包含它:
文件写入:(在我调用 dismissGrantingAccessToURL 之前在文档提供程序中调用:)
NSString *contents = @"this is a dynamically created text file";
NSLog(@"write to file = %@", fileName);
NSError *err;
[contents writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:&err];
文件读取:(在 documentPicker 的主机应用程序内部调用:didPickDocumentAtURL:)
NSString *readback = [[NSString alloc] initWithContentsOfFile:[url path]
usedEncoding:nil
error:&err];
我从 Apple 的示例代码开始,并通读了他们的文档,但无法解释失败的原因。
对于我的文件提供者,只有 init: 函数被调用。我不确定这是否正常,或者是否应该为 Open 操作调用其他方法。这是我的初始化代码:
- (instancetype)init {
self = [super init];
if (self) {
[self.fileCoordinator coordinateWritingItemAtURL:[self documentStorageURL] options:0 error:nil byAccessor:^(NSURL *newURL) {
// ensure the documentStorageURL actually exists
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtURL:newURL withIntermediateDirectories:YES attributes:nil error:&error];
}];
}
return self;
}
我已经验证了上面的错误是零,新的URL如下:
file:///private/var/mobile/Containers/Shared/AppGroup/41EDED34-B449-4FE0-94BA-4046CC573544/File%20Provider%20Storage/
更新:我开始明白为什么主机应用程序无法读取文件了。我认为这是因为我没有托管应用程序中该应用程序组的权限。但是,我认为尝试添加此权限没有意义,因为主机应用程序应与支持适当扩展的任何文档提供程序一起使用。
我想我的文件提供程序未被调用的原因(除了它的 init:) 是因为检测到 'open' 返回的文件是本地文件,因此不需要复制。要传递本地文件,文档说您必须传递位于 documentStorageURL 内的 URL,但(根据定义?)它将映射到不是的应用程序组可由主机应用程序访问。
所以我不确定这将如何运作。如果有人能阐明我在这里应该做什么,我将不胜感激。
在对文档进行了更多挖掘之后,我终于找到了问题所在:在我尝试访问它之前,我需要在 URL 上调用 "startAccessingSecurityScopedResource"。
参考本文档"Accessing files outside your sandbox"的"requirements"部分:
接受的答案是正确的,这里只是一个即时代码:
guard filePath.startAccessingSecurityScopedResource(),
let data = try? Data(contentsOf: filePath) else { return }
filePath.stopAccessingSecurityScopedResource()
// Do your business