file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/apple.pdf
當我做分享功能的時候, 使用iPhone “文件” APP 選擇iCloud中一個文件 選擇轉發分享至本APP
系統調用一下方法:
//MARK: 收到分享內容
- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
NSLog(@"url = %@",url);
NSString *urlStr = url.absoluteString;
if ([urlStr containsString:@"file://"]) {
//收到分享的文件 當是iCloud路徑下的文件的時候 發現為nil
NSData *data = [NSData dataWithContentsOfURL:url];
}
return YES;
}
這時候我們發現data 為nil
當訪問iCloud文件的時候 屬于訪問安全資源 需要請求復制 并訪問 代碼如下:
[url startAccessingSecurityScopedResource];//fileURL ---> Which FileURL you want to copy
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSFileAccessIntent *readingIntent = [NSFileAccessIntent readingIntentWithURL:url options:NSFileCoordinatorReadingWithoutChanges];
[fileCoordinator coordinateAccessWithIntents:@[readingIntent] queue:[NSOperationQueue mainQueue] byAccessor:^(NSError *error) {
NSData *filePathData;
if (!error) {
// Always get URL from access intent. It might have changed.
NSURL *safeURL = readingIntent.URL;
filePathData = [NSData dataWithContentsOfURL:safeURL];
// here your code to do what you want with this
NSLog(@"獲取到數據:%@",filePathData);
}
[url stopAccessingSecurityScopedResource];
}];
這時候filePathData 已經有值了