iOS 将自己App内的文件通过其他应用打开和接收其他App文件的代码

事件需求:将微信里面的word等文件上传到我们自己的App

1、打开相关的通过其他App的代码和页面

 self.documentController = [UIDocumentInteractionController interactionControllerWithURL: urlDocment];

    self.documentController.delegate = self;// 遵循代理

    self.documentController.UTI = @"com.adobe.pdf"; // 哪类文件支持第三方打开,这里不证明就代表所有文件!

//NSString *fileName = url.lastPathComponent;

    self.documentController.name = @"111.pdf";

    [self.documentController presentOpenInMenuFromRect:self.viewController.view.bounds inView:self.viewController.view animated:YES];


2、app想要接收微信共享出来的文件,需要进行相关配置 

   1、info.plist里面配置

<key>CFBundleDocumentTypes</key>


        <dict>

            <key>CFBundleTypeIconFiles</key>

            <array>

                <string>icon.png</string>

                <string>icon@2x.png</string>

            </array>

            <key>CFBundleTypeName</key>

            <string>com.myapp.common-data</string>

            <key>LSItemContentTypes</key>

            <array>

                <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>

                <string>public.text</string>

                <string>public.data</string>

            </array>

        </dict>

参考图片 

- (BOOL)application:(UIApplication*)applicationopenURL:(NSURL*)urlsourceApplication:(NSString*)sourceApplicationannotation:(id)annotation

{

    /*外部文件访问本应用,会传递参数过来*/

    NSLog(@"application = %@",application);

    NSLog(@"url = %@",url);

    NSLog(@"sourceApplication = %@",sourceApplication);

    NSLog(@"annotation = %@",annotation);


   /*得到微信传来的相关文件数据*/

    NSData *data = [[NSData alloc] initWithContentsOfURL:url];

    NSLog(@"%@",data);

   /*将数据保存到我们自己的App的沙盒*/

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    BOOL atomically = [data writeToFile:[NSString stringWithFormat:@"%@/%@",path,@"111.pdf"] atomically:YES]; 

}

// 当文件名为中文时,解决url编码问题

- (NSString*)URLDecodedString:(NSString*)str {

    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    NSLog(@"decodedString = %@",decodedString);

    returndecodedString;

}

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