file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/apple.pdf
當(dāng)我做分享功能的時候, 使用iPhone “文件” APP 選擇iCloud中一個文件 選擇轉(zhuǎn)發(fā)分享至本APP
系統(tǒng)調(diào)用一下方法:
//MARK: 收到分享內(nèi)容
- (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://"]) {
//收到分享的文件 當(dāng)是iCloud路徑下的文件的時候 發(fā)現(xiàn)為nil
NSData *data = [NSData dataWithContentsOfURL:url];
}
return YES;
}
這時候我們發(fā)現(xiàn)data 為nil
當(dāng)訪問iCloud文件的時候 屬于訪問安全資源 需要請求復(fù)制 并訪問 代碼如下:
[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(@"獲取到數(shù)據(jù):%@",filePathData);
}
[url stopAccessingSecurityScopedResource];
}];
這時候filePathData 已經(jīng)有值了