GitHub地址:FileAccess_iCloud_QQ_Wechat
操作
點擊列表跳轉到QQ克懊,微信忱辅。選擇文件,選擇“用其他方式打開”谭溉,點擊原程序圖標墙懂,跳轉回原程序獲取到文件數(shù)據。點擊跳轉到iCloud Drive夜只,選擇文件垒在,跳轉回原程序獲取到文件數(shù)據蒜魄。
效果圖
效果演示.png
截圖.jpeg
截圖2.jpeg
實現(xiàn)步驟
- QQ扔亥,微信
設置白名單
image.png
跳轉
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mqq://"] options:@{} completionHandler:nil];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"] options:@{} completionHandler:nil];
}
設置Document types下的Document Content Type UTIs,指定與程序關聯(lián)的文件類型谈为,詳情參考System-Declared Uniform Type Identifiers
image.png
- iCloud
使用UIDocumentPickerViewController
- (void)presentDocumentPicker {
NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];
UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes
inMode:UIDocumentPickerModeOpen];
documentPickerViewController.delegate = self;
[self presentViewController:documentPickerViewController animated:YES completion:nil];
}
在UIDocumentPickerDelegate里拿到url
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url;
判斷iCloud是否可用
+ (BOOL)iCloudEnable {
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
if (url != nil) {
return YES;
}
NSLog(@"iCloud 不可用");
return NO;
}
通過UIDocument的子類ZHDocument旅挤,傳入url獲取數(shù)據
+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block {
ZHDocument *iCloudDoc = [[ZHDocument alloc]initWithFileURL:url];
[iCloudDoc openWithCompletionHandler:^(BOOL success) {
if (success) {
[iCloudDoc closeWithCompletionHandler:^(BOOL success) {
NSLog(@"關閉成功");
}];
if (block) {
block(iCloudDoc.data);
}
}
}];
}
ZHDocument
#import "ZHDocument.h"
@implementation ZHDocument
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError {
self.data = contents;
return YES;
}
@end
在UIDocumentPickerDelegate依次執(zhí)行,獲取文件名伞鲫,文件數(shù)據粘茄,寫入沙盒Documents
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
NSString *fileName = [array lastObject];
fileName = [fileName stringByRemovingPercentEncoding];
if ([iCloudManager iCloudEnable]) {
[iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {
NSData *data = obj;
//寫入沙盒Documents
NSString *path = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];
[data writeToFile:path atomically:YES];
}];
}
}