本文以 Word、Excel、PPT馍乙、PDF 為例哑芹,其他格式類似處理
1. 在 info.plist 中添加以下設(shè)置,表示可以接收的文件類型
image.png
- 在最新版本中需額外添加 UISupportsDocumentBrowser 并設(shè)置為YES,否則上傳 App Store 會有警告
- 更多類型請參考官方 UTL:傳送門
- 代碼如下
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>OFFICE Document</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.word.doc</string>
<string>org.openxmlformats.wordprocessingml.document</string>
<string>com.microsoft.excel.xls</string>
<string>org.openxmlformats.spreadsheetml.sheet</string>
<string>com.microsoft.powerpoint.ppt</string>
<string>org.openxmlformats.presentationml.presentation</string>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
<key>UISupportsDocumentBrowser</key>
<true/>
2. 在 AppDelegate 中實現(xiàn) application:openURL:options: 方法唠梨,用以接收外界文件
#pragma mark - 外界文件導入
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if (url == nil) {
return NO;
}
NSLog(@"文件地址 - %@", url);
//具體存儲或其他處理在這里進行
}
3. 文檔預覽
- 目前最常用的預覽方式有兩種棵譬,分別是 UIDocumentInteractionController 和 QLPreviewController
3.1 UIDocumentInteractionController
- 不是真正的控制器,繼承自 NSObject末购,使用需要設(shè)置代理
- 一次只能預覽一個文件
//創(chuàng)建
UIDocumentInteractionController *documentVc = [UIDocumentInteractionController interactionControllerWithURL:url];
//設(shè)置代理 UIDocumentInteractionControllerDelegate
documentVc.delegate = self;
//顯示
[documentVc presentPreviewAnimated:YES];
- 實現(xiàn)代理
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
//不實現(xiàn)此代理破喻,默認系統(tǒng)樣式,帶分享功能盟榴,實現(xiàn)時隱藏系統(tǒng)分享
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
return self.view.bounds;
}
3.2 QLPreviewController
- 需導入 #import <QuickLook/QuickLook.h>
- 一次可預覽多個文件曹质,顯示效果與 UIDocumentInteractionController 一樣
-
正常樣式,顯示系統(tǒng)分享按鈕,繼承于 QLPreviewController
@interface PreviewViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate> /// 預覽文檔地址 @property (nonatomic, copy) NSString *filePath; @end @implementation PreviewViewController - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; self.dataSource = self; } - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller { return 1; } - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index { return [NSURL fileURLWithPath:self.filePath]; } //不打開文檔內(nèi)的鏈接 - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item { return NO; } @end
-
隱藏樣式羽德,隱藏系統(tǒng)分享按鈕
QLPreviewController *previewVC = [[QLPreviewController alloc] init]; previewVC.dataSource = self; previewVC.delegate = self; // 將QLPreviewControler 添加到控制器上几莽,隱藏系統(tǒng)分享按鈕 [self addChildViewController:previewVC]; [previewVC didMoveToParentViewController:self]; previewVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [self.view addSubview:previewVC.view]; //其他設(shè)置代理等操作與上面一致
- QLPreviewController 其他代理不再贅述,詳細可查看具體 API