iOS通过UIDocumentInteractionController实现应用间传文件

来自:网络
时间:2023-07-25
阅读:
目录

引言

话开篇:由于iOS沙盒机制,APP文件存储位置只能当前应用访问,这里简单记录一下用 UIDocumentInteractionController 实现APP间传文件。

一、实现效果

iOS通过UIDocumentInteractionController实现应用间传文件

两个 APPTestProjectA 将文件通过 UIDocumentInteractionController 来传递到 TestProjectB

二、配置工程

要想通过系统 UIDocumentInteractionController 功能展示指定的APP,那么,需要在指定的工程 Info.plist 加入如下信息:

iOS通过UIDocumentInteractionController实现应用间传文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version="1.0" >
    <dict>
        <key> CFBundleDocumentTypes </key>
        <array>
            <dict>
                <key> LSHandlerRank </key>
                <string> Default </string>
                <key> LSItemContentTypes </key>
                <array>
                <string> com.adobe.pdf </string>
                    <string> public.data </string>
                    <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>
                </array>
            </dict>
        </array>
    </dict>
</plist>

三、用法

1、弹出文件其他打开方式工具栏

APP-A

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileUrl];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

2、接收文件

APP-B

其实这里的所说的 "接收文件" 是有些不妥的,因为,当 AppDelegate 的方法里获取到文件的沙盒路径已经是 APP-B 的了,这里只是拿来就用。

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if ([url.scheme isEqualToString:@"file"]) {
        NSString * replaceStr;
        #if TARGET_IPHONE_SIMULATOR//模拟器
        replaceStr = @"file://";
        #elif TARGET_OS_IPHONE//真机
        replaceStr = @"file:///private";
        #endif
        NSString * filePathStr = [[NSString stringWithFormat:@"%@",url] stringByReplacingOccurrencesOfString:replaceStr withString:@""];
        /** 业务逻辑 **/
    }
    return YES;
}

内容仅为简单记录,并不是什么新的技术。只是在开发的时候需要时权当个笔记。

以上就是iOS通过UIDocumentInteractionController实现应用间传文件的详细内容,更多关于iOS应用间传文件的资料请关注其它相关文章!

返回顶部
顶部