No permission to read iCloud Drive file

女生的网名这么多〃 提交于 2020-12-06 04:32:13

问题


I am trying to make it possible for my app to open iCloud Drive files, when the user taps on the file in the Apple "Files" app, and selects my app to view it in. Everything is set up fine, such that the app gets opened, and notified when the user taps on the file. But it is not possible to open the file, with the error that the app has no permissions to do so.

My app has "Supports opening documents in place" in the Info.plist set to YES. This means the URL points to an iCloud directory outside the app, which is probably the cause of the problem. If I set that to NO, then URL is a copy of the file inside the app, and everything works fine. But the app needs that setting to be YES.

I have verified, and the file is actually physically present on the phone.

Anybody knows how to make this work?

My openURL: method currently looks like:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    NSLog(@"path=%@", url);
    NSError *error = nil;
    NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
    NSLog(@"error=%@", error);
}

The error that is given is:

Error Domain=NSCocoaErrorDomain Code=257 "The file “Kili.gpx” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Kili.gpx, NSUnderlyingError=0x283ac83f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}


回答1:


I found the solution. You need to surround the reading of the url with startAccessingSecurityScopedResource and stopAccessingSecurityScopedResource:

[url startAccessingSecurityScopedResource];
NSError *error = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
[url stopAccessingSecurityScopedResource];


来源:https://stackoverflow.com/questions/57596871/no-permission-to-read-icloud-drive-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!