iOS 文件应用打开

作者: 青鸟evergreen | 来源:发表于2017-07-27 11:06 被阅读41次

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

相关文章

  • iOS 文件应用打开

    iOS因为沙盒机制的缘故,无法直接应用之间的文件。解决此问题的途径两种,一个是将文件通过iCloud的方式存储,然...

  • iOS下架应用 与 查看手机UDID 与 xcode清除最近打

    iOS下架应用 查看手机UDID XCode清除最近打开的文件列表

  • iOS开发之应用接收Airdrop文件

    参考文章:pengrunPRAirDropDemo iOS中打开的文件如何用其他应用打开选择自己的appiOS接收...

  • 《H5 App开发》HBuilder打包成app 状态栏的颜色问

    沉积式样式(透明) ios: 打开应用的manifest.json文件,切换到代码视图,在plus -> dist...

  • 沙盒

    一、iOS沙盒机制介绍(1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 ...

  • 清理缓存

    每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用的文件系统隔离,ios系统不允许访问其他应用...

  • iOS数据存储

    每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用的文件系统隔离,ios系统不允许访问其他应用...

  • 【从简】秒懂iOS文件分享

    在我看来,iOS的文件分享可分为两种: 1 在你的应用对某个文档如PDF提供可以选择第三方应用的列表来打开该文件功...

  • IOS 数据持久化

    IOS 数据持久化的各种方式 1.plist文件存储 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录)...

  • iOS 的沙盒路径&文件操作

    每个iOS 应用都有自己的沙盒路径,应用沙盒就是文件系统目录,与其他应用的文件系统隔离,iOS应用不允许访问其他应...

网友评论

    本文标题:iOS 文件应用打开

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