iOS App文件共享

1.info.plist注册文件类型

info.plist文件中,添加一个新的属性CFBundleDocumentTypes,这是一个数组类型的属性,这里可以注册等多个类型。

1x.png

或者在info.plist文件中以Source code的方式添加以下代码:

    CFBundleDocumentTypes
    
        
            CFBundleTypeName
            BIN
            LSHandlerRank
            Owner
            LSItemContentTypes
            
                com.apple.macbinary-​archive
            
        
        
            CFBundleTypeName
            PDF
            LSHandlerRank
            Owner
            LSItemContentTypes
            
                com.adobe.pdf
            
        
        
            CFBundleTypeName
            Microsoft Word
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.microsoft.word.doc
                com.microsoft.word.wordml
                org.openxmlformats.wordprocessingml.document
            
        
        
            CFBundleTypeName
            Microsoft Excel
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.microsoft.excel.xls
                org.openxmlformats.spreadsheetml.sheet
            
        
        
            CFBundleTypeIconFiles
            
            CFBundleTypeName
            Microsoft PowerPoint
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.microsoft.powerpoint.​ppt
                org.openxmlformats.presentationml.presentation
                public.presentation
            
        
        
            CFBundleTypeName
            Text
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.text
                public.plain-text
                public.utf8-plain-text
                public.utf16-external-plain-​text
                public.utf16-plain-text
                com.apple.traditional-mac-​plain-text
                public.source-code
                public.c-source
                public.objective-c-source
                public.c-plus-plus-source
                public.objective-c-plus-​plus-source
                public.c-header
                public.c-plus-plus-header
                com.sun.java-source
                public.script
                public.shell-script
            
        
        
            CFBundleTypeName
            Rich Text
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.rtf
                com.apple.rtfd
                com.apple.flat-rtfd
            
        
        
            CFBundleTypeName
            HTML
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.html
                public.xhtml
            
        
        
            CFBundleTypeName
            Web Archive
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.apple.webarchive
            
        
        
            CFBundleTypeName
            Image
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.image
            
        
        
            CFBundleTypeName
            iWork Pages
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.apple.page.pages
                com.apple.iwork.pages.pages
                com.apple.iwork.pages.template
            
        
        
            CFBundleTypeName
            iWork Numbers
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.apple.numbers.numbers
                com.apple.iwork.numbers.numbers
                com.apple.iwork.numbers.template
            
        
        
            CFBundleTypeName
            iWork Keynote
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                com.apple.keynote.key
                com.apple.iwork.keynote.key
                com.apple.iwork.keynote.kth
            
        
        
            CFBundleTypeName
            Audio
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.audio
            
        
        
            CFBundleTypeName
            Movie
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.movie
            
        
        
            CFBundleTypeName
            Archive
            LSHandlerRank
            Alternate
            LSItemContentTypes
            
                public.archive
            
        
    

或者可以根据自己的需求更改,在这里有文件后缀和 UTI 的对应表,可以查看是否支持文件共享。添加好键值对之后就代表已经成功的注册好了App支持的文件类型,运行Xcode,然后再到第三方App如QQ打开一个QQ不支持打开的文件,会显示用其他应用打开,点击的话会出现系统的弹框:

2x.png

2.获取共享文件

#define HSFolderName @"Inbox"
- (NSArray *)getAllFile
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:HSFolderName];
    NSArray *files = [fileManager contentsOfDirectoryAtPath:path error:nil];
    NSMutableArray *dataSource = [NSMutableArray array];

    if (files.count < 1) {
        return dataSource;
    }

    for (NSString *fileString in files) {
        FileShareModel *model = [[FileShareModel alloc] init];
        if ([fileString rangeOfString:@".bin"].location !=NSNotFound) {
            model.fileUrl =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",HSFolderName,fileString]];
            model.fileName = fileString;
            [dataSource addObject:model];
        }
    }
    return dataSource;
}

Demo

你可能感兴趣的:(iOS App文件共享)