一、背景
我們的應(yīng)用下載了一個(gè)文件严嗜,格式是我們自身不支持打開(kāi)的粱檀,怎么辦!Apple中提供了一種方法漫玄,可以使用第三方所支持打開(kāi)這種格式的App來(lái)打開(kāi)茄蚯,如下效果圖:
那我們應(yīng)該怎樣來(lái)設(shè)置這種效果呢压彭,跟著下面的步驟,你就可以輕易地實(shí)現(xiàn)了:
二渗常、分享文件實(shí)現(xiàn)步驟
首先我們要聲明一個(gè)類壮不,必須是全局的!皱碘!不然后面會(huì)發(fā)生崩潰
<pre>
// 定義询一,并遵循代理
@interface ViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
@end
</pre>
在我們需要觸發(fā)共享文件的方法里面實(shí)現(xiàn)下面的代碼:
<pre>
- (void)buttonOnClick:(UIButton*)btn{
//初始化 filePath:為所要打開(kāi)的文件的路徑
_documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_documentController.delegate = self;// 遵循代理
_documentController.UTI = @"com.microsoft.excel.xls"; // 哪類文件支持第三方打開(kāi),這里不證明就代表所有文件癌椿!
[_documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];
}
</pre>
實(shí)現(xiàn)代理方法
<pre>
pragma mark - ********** UIDocumentInteractionController 代理方法 **********
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
} - (UIView)documentInteractionControllerViewForPreview:(UIDocumentInteractionController)controller {
return self.view;
} - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
return self.view.frame;
}
</pre>
最后在Build Phases中的Link Binary中導(dǎo)進(jìn)QuickLook.framework這個(gè)庫(kù)
最后我們就可以通過(guò)我們的的方法來(lái)共享文件出去了健蕊。
------------------- 分割線 -------------------
當(dāng)然,我們也可以通過(guò)下面的方法如失,直接打開(kāi)文檔绊诲,UIDocumentInteractionController本身也支持部分文檔的預(yù)覽,例如Word、excel褪贵、js等等:
<pre>
- (void)readButtonOnClick:(UIButton*)btn{
//初始化 filePath:為所要打開(kāi)的文件的路徑
_documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_documentController.delegate = self;// 遵循代理
// 直接打開(kāi)預(yù)覽文檔
[_documentController presentPreviewAnimated:YES];
}
</pre>
遇到無(wú)法打開(kāi)的文檔的時(shí)候掂之,我們還可以共享
配置中遇到的問(wèn)題:選中共享的應(yīng)用之后,應(yīng)用閃退脆丁!報(bào)錯(cuò)信息如下:(原因是因?yàn)闆](méi)有把類設(shè)置為全局導(dǎo)致提前釋放了)
<pre>
*** Assertion failure in -[_UIOpenWithAppActivity performActivity], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UIDocumentInteractionController.m:408
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController has gone away prematurely!'
*** First throw call stack:
(0x248e185b 0x35fa2dff 0x248e1731 0x25672ddb 0x290638c9 0x292695bb 0x28d5aefd 0x28d5e1a1 0x28b42107 0x28a50a55 0x28a50531 0x28a5042b 0x282e05cf 0x1acd03 0x1b17c9 0x248a4535 0x248a2a2f 0x247f50d9 0x247f4ecd 0x2db6aaf9 0x28a7e2dd 0x780ad 0x366f0873)
libc++abi.dylib: terminating with uncaught exception of type NSException
</pre>
三世舰、接收別的App傳過(guò)來(lái)的文件
我們能夠通過(guò)把文件傳給別的應(yīng)用打開(kāi),這樣子那我們的App要怎樣做才能支持別的程序傳文件過(guò)來(lái)打開(kāi)呢槽卫?
下面我們以支持打開(kāi)PDF為例子:其他文檔類型可以去在官網(wǎng)找https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
在AppDelegate.m文件中跟压,重寫(xiě)以下方法,就可以獲取到傳過(guò)來(lái)的內(nèi)容:
<pre>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if (url != nil) {
NSString *path = [url absoluteString];
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file://"]) {
[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
}
// 打開(kāi)PDF文檔
NSlog(@"獲取到的文件:%@", string);
}
return YES;
}
</pre>