0

当使用本机应用程序“文件”选择文件时,调用我的应用程序并获取路径:

file:///private/var/mobile/Containers/Shared/AppGroup/F9BF9D9E-4756-40ED-85A4-810B4D1A67CB/File%20Provider%20Storage/myFile.abcd

我在字符串“originalPath”中捕捉到路径

NSURL *originalURL = [NSURL URLWithString:originalPath];
NSURL *destinationURL = [NSURL URLWithString:destination]; //This is a path in the cache folder of my app

BOOL success = [originalURL startAccessingSecurityScopedResource];

NSError *error;

NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];

[fileCoordinator coordinateReadingItemAtURL:originalURL options:NSFileCoordinatorReadingWithoutChanges writingItemAtURL:destinationURL  options:NSFileCoordinatorWritingForReplacing  error:&error byAccessor:^(NSURL *newURL, NSURL *newWritingURL) {

[[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:newWritingURL.path error:&error];

if (success) {
   [originalURL stopAccessingSecurityScopedResource];
 }
}
];

error.localizerDescription 的值

The file “myFile.abcd” couldn’t be opened because you don’t have permission to view it.

在控制台中我可以看到:

filecoordinationd   Claim 97D67388-49D7-4076-9622-0FD18C9DE063 granted in server    14:15:47.421475-0700
filecoordinationd   Claimer for 97D67388-49D7-4076-9622-0FD18C9DE063 is waiting for provider com.apple.FileProvider.LocalStorage to provide 14:15:47.421495-0700
filecoordinationd   Hit the server for a process handle 9fc643c000014c6 that resolved to: [application<com.mydomain.myappname>:5318]    14:15:47.421516-0700
filecoordinationd   Caching handle <private>, with ipc id 9fc643c000014c6, and pid 5318 14:15:47.421535-0700
fileproviderd   com.apple.FileProvider.LocalStorage providing <private> for claim 97D67388-49D7-4076-9622-0FD18C9DE063  14:15:47.421832-0700
fileproviderd   com.apple.FileProvider.LocalStorage began providing 14:15:47.421853-0700
filecoordinationd   Claim 97D67388-49D7-4076-9622-0FD18C9DE063 granted in server    14:15:47.421475-0700
filecoordinationd   Claimer for 97D67388-49D7-4076-9622-0FD18C9DE063 is waiting for provider com.apple.FileProvider.LocalStorage to provide 14:15:47.421495-0700
filecoordinationd   Hit the server for a process handle 9fc643c000014c6 that resolved to: [application<com.mydomain.myappname>:5318]    14:15:47.421516-0700
filecoordinationd   Caching handle <private>, with ipc id 9fc643c000014c6, and pid 5318 14:15:47.421535-0700
fileproviderd   com.apple.FileProvider.LocalStorage providing <private> for claim 97D67388-49D7-4076-9622-0FD18C9DE063  14:15:47.421832-0700
fileproviderd   com.apple.FileProvider.LocalStorage began providing 14:15:47.421853-0700
filecoordinationd   Claim D578716E-D91A-4ECB-9348-CF9824341DA9 invoked in server    14:15:47.421862-0700
myAppName   Claim D578716E-D91A-4ECB-9348-CF9824341DA9 granted in client    14:15:47.421880-0700
myAppName   Claim D578716E-D91A-4ECB-9348-CF9824341DA9 invoked in client    14:15:47.421923-0700

在调用 startAccessingSecurityScopedResource 时总是得到 FALSE,在 fileCoordinator 中的访问器内永远看不到 originaURL。

有没有其他方法可以读取文件,我可以请求权限吗?

4

0 回答 0