iOS 文件应用打开

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

iOS 文件应用打开_第1张图片
在其他应用中打开

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

CFBundleDocumentTypes
    
        
            CFBundleTypeName
            com.myapp.common-data
            LSItemContentTypes
            
                com.microsoft.powerpoint.ppt
                public.item
                com.microsoft.word.doc
                com.adobe.pdf
                com.microsoft.excel.xls
                public.image
                public.content
                public.composite-content
                public.archive
                public.audio
                public.movie
                public.text
                public.data
            
        
    

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

- (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 文件应用打开)