最近公司有這樣需求:當(dāng)用戶用微信,qq或者其他app接Word,Ecxel,PDF文件時,選擇打開方式時,自己的app能出現(xiàn)打開方式列表中,并能跳轉(zhuǎn)到自己的app打開或者上傳此類文件,解決辦法如下:
部分代碼參考自 http://stackoverflow.com/questions/15836145/associate-files-type-with-my-iphone-app
1.在info.plist文件作如下配置
- 1.1 新增 CFBundleDocumentTypes 這個key,它對應(yīng)的value是一個字典數(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這個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)建一個控制器用于處理這些文件,并在AppDelegate方法中作監(jiān)聽這些文件的打開
- 2.1 我是創(chuàng)建一個普通控制器QYFileReaderController,控制器的有一個UIWebView屬性,用UIWebView就可以自動解析這些文件
- 2.2 QYFileReaderController對外暴露一個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;
}