iOS File文件共享

在Files显示自己的APP

info.plist 添加Supports opening documents in place(LSSupportsOpeningDocumentsInPlace)
就可以在On My iPhone看到自己的APP,如果没有的话,就打开相册随便分享一个图片到Save to Files就会出现

Browse

On My iPhone

Application supports iTunes file sharing(UIFileSharingEnabled)是指把Documents共享给iTunes,连上数据线可以在文件共享看到对应App的Document文件夹的数据,因此对一些私密的文件来说是不安全的。所以想开共享,又不想暴露太多文件数据的话,需要调整项目文件的读取路径。
如果不是那么重要的文件,我们可以将它们存放在 NSCachesDirectory 或者是 NSTemporaryDirectory 文件夹下面;如果它是重要的文件,大多数情况下,我们是需要将它们备份在 iCloud 上的,这样的文件我们建议将它存放在 NSApplicationSupportDirectory目录下。

UIFileSharingEnabled


第三方App调用自己的App打开

调用自己的App

针对系统相册处理方式与其他APP并不一样,下面方式是针对一般第三方App。系统相册需要添加Share Extension,可以看这篇文章

在区域1显示自己的APP
info.plist中添加

    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>Type Name</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Default</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.example.plain-text</string>
            </array>
        </dict>
    </array>

CFBundleDocumentTypes只要有数据,会在target setting info中相对应显示。

Document Types

CFBundleTypeName:文档的类型名称(自定义输入)
Handler rank:字符串类型,包含Owner,Default,Alternate,None四个可选值,指定对于某种类型的优先权级别,而Launcher Service会根据这个优先级别来排列显示的App的顺序。优先级别从高到低依次是OwnerAlternateDefaultNone表示不接受这种类型。

LSItemContentTypes根据UTIs lists定义,并非乱填。

基本上用

<string>com.microsoft.powerpoint.ppt</string>
<string>com.microsoft.word.doc</string>
<string>com.microsoft.excel.xls</string>
<string>com.adobe.pdf</string>
<string>public.item</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>

就能涵盖大部分常用类型,其它的字段都可以随意添加,会显示在Additional document type properties

第三方应用打开文件会调用下面的代理方法

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
    if (self.window) {
        if (url) {
            NSString *fileNameStr = [url lastPathComponent];
            NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];
            NSData *data = [NSData dataWithContentsOfURL:url];
            [data writeToFile:Doc atomically:YES];
            NSLog(@"文件已存到本地文件夹内");
        }
    }
    return YES;
}

这样就可以根据自己的业务做保存或打开。


在区域2显示自己的APP
target中添加新targetAction Extension

Action Extension

之后看这篇文章(//www.greatytc.com/p/37f23426bb04)


info.plist其它key解读

UISupportsDocumentBrowser
如果应用使用UIDocumentInteractionController来打开文件,info.plist就要将 UISupportsDocumentBrowser设置为YES

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

推荐阅读更多精彩内容