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

引言

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

一、实现效果

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

二、配置工程

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

iOS通过UIDocumentInteractionController实现应用间传文件_第1张图片



    
         CFBundleDocumentTypes 
        
            
                 LSHandlerRank 
                 Default 
                 LSItemContentTypes 
                
                 com.adobe.pdf 
                     public.data 
                     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 
                
            
        
    

三、用法

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 *)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应用间传文件的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(iOS通过UIDocumentInteractionController实现应用间传文件)