zoukankan      html  css  js  c++  java
  • ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件

    1.自己的APP调用第三方打开文件

    主要是使用  UIDocumentInteractionController  类   并实现 UIDocumentInteractionControllerDelegate 的代理方法

    @interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>
    
    @property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
    
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //url 为需要调用第三方打开的文件地址
        NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]];
            _documentInteractionController = [UIDocumentInteractionController
                                                  interactionControllerWithURL:url];
            [_documentInteractionController setDelegate:self];
            
            [_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    }

    需要在真机上调试,例子中打开的是  doc文件,如果手机上装了WPS或者office套件,就能调用这些应用打开。

    2.第三方APP调用自己的APP,打开文件

    在info.plist中添加如下代码

     1     <array>
     2         <dict>
     3             <key>CFBundleTypeName</key>
     4             <string>com.myapp.common-data</string>
     5             <key>LSItemContentTypes</key>
     6             <array>
     7                 <string>com.microsoft.powerpoint.ppt</string>
     8                 <string>public.item</string>
     9                 <string>com.microsoft.word.doc</string>
    10                 <string>com.adobe.pdf</string>
    11                 <string>com.microsoft.excel.xls</string>
    12                 <string>public.image</string>
    13                 <string>public.content</string>
    14                 <string>public.composite-content</string>
    15                 <string>public.archive</string>
    16                 <string>public.audio</string>
    17                 <string>public.movie</string>
    18                 <string>public.text</string>
    19                 <string>public.data</string>
    20             </array>
    21         </dict>
    22     </array>

    这在系统中添加了参数,如果有以上类型的文件,第三方应用可以调用我们的APP进行操作。

    在第三方调用我们的APP后,会调用如下方法

    
    
     1 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
     2 {
     3     if (self.window) {
     4         if (url) {
     5             NSString *fileNameStr = [url lastPathComponent];
     6             NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];
     7             NSData *data = [NSData dataWithContentsOfURL:url];
     8             [data writeToFile:Doc atomically:YES];
     9             [XCHUDTool showOKHud:@"文件已存到本地文件夹内" delay:2.0f];
    10         }
    11     }
    12     return YES;
    13 }
    
    

    url 就是第三方应用调用时文件的沙盒地址,

    @"Documents/localFile" 表示本地文件夹目录

    sourceApplication 是调用我们APP的第三方应用是谁

    我们把url传到我们需要用的界面

     可以使用路径查看保存到本地的文件

  • 相关阅读:
    原码/反码/补码详解
    树剖LCA讲解
    code vs 2166 Bessie的体重问题
    code vs 3376 符号三角形
    洛谷 P3078 [USACO13MAR]扑克牌型Poker Hands
    vjudge B
    POJ 1013 Counterfeit Dollar
    POJ 1008 Maya Calendar
    POJ 1006 Biorhythms
    POJ 3252 Round Numbers
  • 原文地址:https://www.cnblogs.com/zhanghuanan/p/5311640.html
Copyright © 2011-2022 走看看