最近公司有这样需求:当用户用微信,qq或者其他app接Word,Ecxel,PDF文件时,选择打开方式时,自己的app能出现打开方式列表中,并能跳转到自己的app打开或者上传此类文件,解决办法如下:
部分代码参考自 http://stackoverflow.com/questions/15836145/associate-files-type-with-my-iphone-app
1.在info.plist文件作如下配置
- 1.1 新增 CFBundleDocumentTypes 这个key,它对应的value是一个字典数组,具体配置如下
<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.创建一个控制器用于处理这些文件,并在AppDelegate方法中作监听这些文件的打开
- 2.1 我是创建一个普通控制器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];
// 调用
[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];
// 自动适应大小
self.webView.scalesPageToFit = YES;
// 关闭弹簧效果
self.webView.scrollView.bounces = NO;
}
- 2.3 在AppDelegate的 (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url方法中监听这些文件的打开和获取对应文件的url,并传递url给QYFileReaderController
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// NSLog(@"url:%@",url.absoluteString);
// NSLog(@"host:%@",url.host);
BOOL isLogin = YES; // 假设用户已登录
if (isLogin)
{
QYFileReaderController *readerVC = [[QYFileReaderController alloc] init];
readerVC.absoluteUrl = url;
UINavigationController *nav = self.window.rootViewController;
[nav pushViewController:readerVC animated:YES];
}
return YES;
}