讓自己的app出現(xiàn)在其它應(yīng)用的打開方式列表中

最近公司有這樣需求:當(dāng)用戶用微信,qq或者其他app接Word,Ecxel,PDF文件時(shí),選擇打開方式時(shí),自己的app能出現(xiàn)打開方式列表中,并能跳轉(zhuǎn)到自己的app打開或者上傳此類文件,解決辦法如下:

以下只是示例,具體可以參考 https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

部分代碼參考自 http://stackoverflow.com/questions/15836145/associate-files-type-with-my-iphone-app

1.在info.plist文件作如下配置

  • 1.1 新增 CFBundleDocumentTypes 這個(gè)key,它對應(yīng)的value是一個(gè)字典數(shù)組,具體配置如下
<key>CFBundleDocumentTypes</key>
 <array>
    <dict>
       <key>CFBundleTypeName</key>
           <string>Molecules Structure File</string>
       <key>CFBundleTypeIconFiles</key>
       <array>
          <string>icon@2x.png</string>
          <string>icon.png</string>
       </array>
      <key>LSItemContentTypes</key>
         <array>
            <string>com.adobe.pdf</string>
            <string>com.microsoft.word.doc</string>
            <string>com.microsoft.excel.xls</string>
         </array>
     <key>CFBundleTypeRole</key>
         <string>Viewer</string>
     <key>LSHandlerRank</key>
       <string>Owner</string>
    </dict>
 </array>
  • 1.2 聲明支持的文件類型和文件拓展名,新增UTExportedTypeDeclarations這個(gè)key,配置如下
<key>UTExportedTypeDeclarations</key>
 <array>
  <dict>
   <key>UTTypeConformsTo</key>
   <array>
    <string>public.data</string>
    <string>public.composite-content</string>
   </array>
   <key>UTTypeIdentifier</key>
   <string>com.adobe.pdf</string>
   <key>UTTypeDescription</key>
   <string>PDF文檔</string>
   <key>UTTypeTagSpecification</key>
   <dict>
    <key>public.mime-type</key>
    <string>application/pdf</string>
    <key>public.filename-extension</key>
    <array>
     <string>pdf</string>
    </array>
   </dict>
  </dict>
  <dict>
   <key>UTTypeConformsTo</key>
   <array>
    <string>public.data</string>
   </array>
   <key>UTTypeIdentifier</key>
   <string>com.microsoft.word.doc</string>
   <key>UTTypeDescription</key>
   <string>Word文檔</string>
   <key>UTTypeTagSpecification</key>
   <dict>
    <key>public.mime-type</key>
    <string>application/msword</string>
    <key>public.filename-extension</key>
    <array>
     <string>doc</string>
     <string>docx</string>
    </array>
   </dict>
  </dict>
  <dict>
   <key>UTTypeConformsTo</key>
   <array>
    <string>public.data</string>
   </array>
   <key>UTTypeIdentifier</key>
   <string>com.microsoft.excel.xls</string>
   <key>UTTypeDescription</key>
   <string>Excel Document</string>
   <key>UTTypeTagSpecification</key>
   <dict>
    <key>public.mime-type</key>
    <string>application/vnd.ms-excel</string>
    <key>public.filename-extension</key>
    <array>
     <string>xls</string>
    </array>
   </dict>
  </dict>
 </array>

2.創(chuàng)建一個(gè)控制器用于處理這些文件,并在AppDelegate方法中作監(jiān)聽這些文件的打開

  • 2.1 我是創(chuàng)建一個(gè)普通控制器QYFileReaderController,控制器的有一個(gè)UIWebView屬性,用UIWebView就可以自動解析這些文件
  • 2.2 QYFileReaderController對外暴露一個(gè)NSUrl類型的屬性,用于接收這些文件的url
    代碼如下:
@interface QYFileReaderController : UIViewController
/** 文件絕對url */
@property (nonatomic,strong)NSURL *absoluteUrl;
@end
@interface QYFileReaderController ()
@property (weak, nonatomic)  UIWebView *webView;
@end

@implementation QYFileReaderController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupWebView];
    // 調(diào)用
    [self.webView loadRequest:[NSURLRequest requestWithURL:self.absoluteUrl]];
}
   
- (void)setupWebView{
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.webView = webView;
    [self.view addSubview:self.webView];
    
    // 自動適應(yīng)大小
    self.webView.scalesPageToFit = YES;
    
    // 關(guān)閉彈簧效果
    self.webView.scrollView.bounces = NO;    
}
  • 2.3 在AppDelegate的 (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url方法中監(jiān)聽這些文件的打開和獲取對應(yīng)文件的url,并傳遞url給QYFileReaderController

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//    NSLog(@"url:%@",url.absoluteString);
//    NSLog(@"host:%@",url.host);
    BOOL isLogin = YES; // 假設(shè)用戶已登錄
   if (isLogin)
    {
        QYFileReaderController *readerVC = [[QYFileReaderController alloc] init];
        readerVC.absoluteUrl = url;
        UINavigationController *nav = self.window.rootViewController;
        [nav pushViewController:readerVC animated:YES];
    }
    return YES;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容