• iOS调用文件app(file.app)选择文件和下载


    第三方应用调用系统文件app,并下载文件。

    ###配置如下
    1:证书文件配置
    在Identifiers下创建你的iCloud Containers配置,点击“+”创建

    2:在Identifiers下选择你要添加icloud的boundid把icloud配置勾选上既可

    3:打开工程中的配置如下
    工程配置icloud.jpg

    4:可选配置
    在info.plist中添加如下两个配置
    Supports opening documents in place
    Application supports iTunes file sharing
    结果都为YES
    设置完以后,会在文件app中有一个和你工程名相同的文件夹出现。

    5:打开文件app

    - (void)presentDocumentCloud {
        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];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    遵守代理如下

    <UIDocumentPickerDelegate, UIDocumentInteractionControllerDelegate>
    
    • 1

    选择文件事件

    #pragma mark - UIDocumentPickerDelegate
    - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
        
        NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
        NSString *fileName = [array lastObject];
        fileName = [fileName stringByRemovingPercentEncoding];
        NSLog(@"--->>>>%@",fileName);
        if ([iCloudManager iCloudEnable]) {
            [iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {
                NSData *data = obj;
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"icloud" message:@"写入沙河" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
                //写入沙盒Documents
                NSString *path = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];
                [data writeToFile:path atomically:YES];
            }];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    6:判断icloud是否可用

    + (BOOL)iCloudEnable {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
        if (url != nil) {
            return YES;
        }
        NSLog(@"iCloud 不可用");
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"icloud" message:@"iCloud 不可用" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        return NO;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    下载工程

    有些地方说的不到位,还请各位看官指正。。。

    • 也可以添加洲洲哥的微信公众号

    可以来微信公众号(洲洲哥)后台给我们留言。 快来扫码关注我们吧!

    公众号二维码

  • 相关阅读:
    IP行业API助力于网络分析和数据挖掘
    Rocky9 上安装 redis-dump 和redis-load 命令
    【算法】Reverse Integer
    Windows系统中搭建docker (ubuntu,Docker-desktop)
    数学建模笔记(三)论文写作:论文排版
    基于hydra库实现yaml配置文件的读取(支持命令行参数)
    发布Android库至MavenCentral详解
    【解决】修改 UI界面渲染层级 的常见误区
    93.(cesium篇)cesium动态单体化-倾斜摄影(楼栋)
    实现SSE的textevent-stream是什么?和applicationoctet-stream有什么区别?
  • 原文地址:https://blog.csdn.net/whuizhou/article/details/125434510