美文网首页知识积累可能会用到自己看的文章
iOS - 从iCloud,QQ,微信获取文件

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

作者: Unlimitzzh | 来源:发表于2017-07-28 17:48 被阅读1473次

GitHub地址:FileAccess_iCloud_QQ_Wechat

操作

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

效果图

效果演示.png 截图.jpeg 截图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

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];
        }];
    }
}

相关文章

网友评论

  • 皇军让我给你捎个话:git上为什么没有
  • 梁森的简书:能从iCloud中选择iCloud中的照片吗?
  • 9abf4a84b4cd:大神请教个问题,我想从QQ里面分享个文件到你的app里面,但是这个文件的后缀是.dfu,我在你上面的type链接里面没找到dfu所对应的key,是不是意味着我不能分享此类文件?
  • be2e8d37da16:大神我copy您的代码进项目,直接进入iCloud获取文件,提示 Error Domain=NSFileProviderInternalErrorDomain Code=1 "The reader is not permitted to access the URL." UserInfo={NSLocalizedDescription=The reader is not permitted to access the URL.}
    为什么呢?
  • 长鲜:老兄 为什么你的Github Demo 下载过来没有工程文件 只有源文件啊 打不开
    Unlimitzzh:已经重新上传了
  • 新月如火:从QQ、微信获取文件是怎么个情况?单纯跳转一下就没了?
    新月如火:@Unlimitzzh 嗯呢 懂了,:+1: :+1:
    Unlimitzzh:这里实现的效果是,从QQ、微信选择文件,通过“用其他方式打开”跳转回原程序来获取到该文件数据。你可以下载demo试试看
  • 红颜疯子:可以哦

本文标题:iOS - 从iCloud,QQ,微信获取文件

本文链接:https://www.haomeiwen.com/subject/hqhflxtx.html