iOS 文件应用打开

iOS因为沙盒机制的缘故,无法直接应用之间的文件。解决此问题的途径两种,一个是将文件通过iCloud的方式存储,然后应用的文件可以共享,具体内容不赘述。
第二个方式是通过文件关联的方式,也就是通过UIDocument中的方法打开在应用中打开并存储,表现形式如下:

在其他应用中打开

要做到上述形式,必须先在info.plist中做好文件配置

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>com.myapp.common-data</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.microsoft.powerpoint.ppt</string>
                <string>public.item</string>
                <string>com.microsoft.word.doc</string>
                <string>com.adobe.pdf</string>
                <string>com.microsoft.excel.xls</string>
                <string>public.image</string>
                <string>public.content</string>
                <string>public.composite-content</string>
                <string>public.archive</string>
                <string>public.audio</string>
                <string>public.movie</string>
                <string>public.text</string>
                <string>public.data</string>
            </array>
        </dict>
    </array>

在应用中选取别的应用打开后,会在如下方法获取文件地址,这个地址其实就是在实际打开应用中存储的地址

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSString *path = [url absoluteString];
    NSMutableString *string = [[NSMutableString alloc] initWithString:path];
    if ([path hasPrefix:@"file://"]) {
        [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch  range:NSMakeRange(0, path.length)];   
        NSDictionary * dict = [NSDictionary dictionaryWithObject:path forKey:@"filepath"];
        [[NSNotificationCenter defaultCenter]postNotificationName:KOpenFile object:nil userInfo:dict];
        return YES;
    }
}

在需要获取文件的controller中遵守代理UIDocumentInteractionControllerDelegate并打开文件,在此代理方法并未实现,只是简单的文件预览

if (!_path) {
        return;
    }
    NSURL *fileURL = [NSURL URLWithString:_path];
    
    //创建实例
    self.documentController =
    [UIDocumentInteractionController
     interactionControllerWithURL:fileURL];
    
    //设置代理
    self.documentController.delegate = self;
  //打开文件
    [self.documentController presentPreviewAnimated:YES];

附获取本地文件名称路径及大小

NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:docsDir];
    
    NSString *fileName;
    
    
    while (fileName = [dirEnum nextObject]) {
        NSString * filepath = [docsDir stringByAppendingPathComponent:fileName];
        
        //获取文件属性
        unsigned long  long fileSize =  [[fileManager attributesOfItemAtPath:filepath error:nil]fileSize];
        NSString * size ;
        
        if (fileSize >= pow(10, 9)) { // size >= 1GB
            size = [NSString stringWithFormat:@"%.2fGB", fileSize / pow(10, 9)];
        } else if (fileSize >= pow(10, 6)) { // 1GB > size >= 1MB
            size = [NSString stringWithFormat:@"%.2fMB", fileSize / pow(10, 6)];
        } else if (fileSize >= pow(10, 3)) { // 1MB > size >= 1KB
            size = [NSString stringWithFormat:@"%.2fKB", fileSize / pow(10, 3)];
        } else { // 1KB > size
            size = [NSString stringWithFormat:@"%zdB", fileSize];
        }
        //除掉inbox
        if (fileName.length>6) {
            
            NSString * newfileName = [fileName substringFromIndex:6];
            NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
            [dict setValue:newfileName forKey:@"fileName"];
            [dict setValue:filepath forKey:@"path"];
            [dict setValue:size forKey:@"fileSize"];
            [self.fileArray addObject:dict ];
        }
        
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,436评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,172评论 6 13
  • 「遇见」 白雪恋上山峰 想要白头 星星追逐白昼 不眠不休 故事结尾缠上开头
    wuli榛子酱阅读 249评论 0 0
  • 广告和小资让咖啡走进我的生活 我喝咖啡好几年,中间戒了几次,没有成功,现在才意识到,原来我是一个不适合喝咖啡的人。...
    ef053e4d2a28阅读 328评论 0 1