ios开发之--UIDocumentInteractionController的使用(实现更多分享服务)
最近在做項(xiàng)目的時(shí)候,碰到這樣一個(gè)需求,就是本地生成pdf文件,然后本地打開(kāi),經(jīng)過(guò)測(cè)試發(fā)現(xiàn),pdf文件是無(wú)法保存到相冊(cè)里面的,只能存到手機(jī)里面,鑒于蘋果的存儲(chǔ)機(jī)制,需要取出來(lái),進(jìn)行本地展示,可以直接傳到后臺(tái)生成一個(gè)鏈接,直接在webview和瀏覽器里面打開(kāi),但是效果并不好,加載也慢些,所以就想到了用通過(guò)UIDocumentInteractionController來(lái)實(shí)現(xiàn)本地查看的操作,具體代碼如下:
1,簡(jiǎn)介
UIDocumentInteractionController是從ios 3.2的sdk開(kāi)始支持的,他是直接繼承自NSObject,因此需要UIDocumentInteractionController提供的方法來(lái)展示他,UIDocumentInteractionController主要給我們提供了三種用途:
1 展示一個(gè)可以操作我們分享的文檔類型的第三方App列表2 在第一條展示列表的基礎(chǔ)上添加額外的操作,比如復(fù)制,打印,預(yù)覽,保存等。3 結(jié)合Quick Look框架直接展示文檔內(nèi)容2,簡(jiǎn)單的布局就不說(shuō)了,直接上主要代碼:
初始化:
- (IBAction)shareClick:(id)sender {UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"pdf"]]; }實(shí)現(xiàn)一個(gè)UIDocumentInteractionController的方法:
- (BOOL)presentOpenInMenuFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;在上面的button的點(diǎn)擊方法里面再加入此方法:
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"pdf"]];[documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];然后運(yùn)行工程,進(jìn)行測(cè)試,點(diǎn)擊button,就可以看到如下界面:
?
?這里我做了本地的漢化處理,真機(jī)上會(huì)顯示漢字,具體請(qǐng)參考我寫的“系統(tǒng)控件漢化”的博客。
簡(jiǎn)單介紹下這個(gè)界面:
第一行列表顯示“AirDrop”是蘋果在iOS 7提供的一種跨設(shè)備分享的技術(shù);
第二行列表展示整個(gè)iOS系統(tǒng)中,可以操作PDF文檔的應(yīng)用程序列表,還包括了蘋果在iOS 8提供的Share Extension圖標(biāo);
第三行列表展示現(xiàn)實(shí)設(shè)備可選的操作,如Copy,Print等;
這個(gè)時(shí)候,點(diǎn)擊分享到微信,這個(gè)時(shí)候,會(huì)發(fā)現(xiàn)崩潰了,錯(cuò)誤信息如下:
根據(jù)錯(cuò)誤提示,“***has gone away prematurely”過(guò)早的消失,在ARC環(huán)境下,方法走完之后,UIDocumentInteractionController的實(shí)例被釋放了,展示出來(lái)的這個(gè)view是有Quick Look框架來(lái)操作,不會(huì)對(duì)UIDocumentInteractionController產(chǎn)生引用,所以當(dāng)點(diǎn)擊button的時(shí)候,內(nèi)部操作仍然會(huì)繼續(xù)訪問(wèn)這個(gè)實(shí)例,所以就會(huì)報(bào)過(guò)早的消失這個(gè)錯(cuò)誤!
解決方法:把UIDocumentInteractionController聲明為一個(gè)強(qiáng)指針strong類型的實(shí)例,然后修改下button的觸發(fā)方法即可:
@property(nonatomic,strong)UIDocumentInteractionController *documentController;方法寫成私有方法:
-(void)presentOpenInMenu{
??? [_documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
最終代碼:
- (IBAction)shareClick:(id)sender {_documentController = [UIDocumentInteractionController interactionControllerWithURL:[[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"pdf"]];[self presentOpenInMenu];}這樣就解決了!
展示可選操作,實(shí)現(xiàn)此方法即可:
- (BOOL)presentOptionsMenuFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;封裝私有方法:
-(void)presentOptionsMenus {[_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; }然后在button點(diǎn)擊方法里面替換成上述方法,效果如下圖:
華麗麗的出現(xiàn)了!
?直接預(yù)覽
UIDocumentInteractionController第三種預(yù)覽文檔內(nèi)容的用途就是重點(diǎn),而且也很常見(jiàn),例如微信里面和瀏覽器里面就是用了此種方法,很是方便。
實(shí)現(xiàn)預(yù)覽效果也很方便,實(shí)現(xiàn)一個(gè)代理方法即可:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller;此代理方法主要是用來(lái)指定UIDocumentInteractionController要顯示的視圖所在的父視圖,這樣UIDocumentInteractionController才知道在哪里展示Quick Look預(yù)覽內(nèi)容,當(dāng)然了,這里是指定button所在的VC來(lái)做UIDocumentInteractionController的代理對(duì)象,添加如下代碼:
@interface ViewController ()<UIDocumentInteractionControllerDelegate> _documentController.delegate = self;實(shí)現(xiàn)代理方法:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {return self; }UIDocumentInteractionController是繼承自NSObject的,因而為了能夠?qū)崿F(xiàn)直接預(yù)覽,我們需要用到UIDocumentInteractionController提供的展示預(yù)覽的方法:
- (BOOL)presentPreviewAnimated:(BOOL)animated;私有方法:
-(void)presentPreview {[self.documentController presentPreviewAnimated:YES]; }在button點(diǎn)點(diǎn)擊事件里面添加上此方法即可,效果如下:
?
?這樣所要展示的pdf文件,就華麗麗的展示了出來(lái)!
參考自:http://www.jianshu.com/p/3f03897cf98a
?
轉(zhuǎn)載于:https://www.cnblogs.com/hero11223/p/7306104.html
總結(jié)
以上是生活随笔為你收集整理的ios开发之--UIDocumentInteractionController的使用(实现更多分享服务)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 总是梦到初恋怎么办
- 下一篇: A 子类继承父类,子类的构造函数会覆盖父