iOS - 从iCloud,QQ,微信获取文件

GitHub地址:FileAccess_iCloud_QQ_Wechat

操作

点击列表跳转到QQ,微信。选择文件,选择“用其他方式打开”,点击原程序图标,跳转回原程序获取到文件数据。点击跳转到iCloud Drive,选择文件,跳转回原程序获取到文件数据。

效果图

iOS - 从iCloud,QQ,微信获取文件_第1张图片
效果演示.png
iOS - 从iCloud,QQ,微信获取文件_第2张图片
截图.jpeg
iOS - 从iCloud,QQ,微信获取文件_第3张图片
截图2.jpeg

实现步骤

  • 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,指定与程序关联的文件类型,详情参考System-Declared Uniform Type Identifiers

iOS - 从iCloud,QQ,微信获取文件_第4张图片
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获取数据

+ (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依次执行,获取文件名,文件数据,写入沙盒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];
        }];
    }
}

你可能感兴趣的:(iOS - 从iCloud,QQ,微信获取文件)