設(shè)置為屬性
@property (nonatomic ,strong) UIDocumentInteractionController *documentInteractionController;
創(chuàng)建
- ( void )setupDocumentControllerWithURL:( NSURL *)url
{
if ( self.documentInteractionController == nil ){
self.documentInteractionController = [ UIDocumentInteractionController interactionControllerWithURL :url];
self.documentInteractionController.delegate = self ;
}
else {
self.documentInteractionController.URL = url;
}
}
/**
* 打開文件
*/
-(void)openFileWithPath:(NSString*)path{
NSURL* URL = [NSURL fileURLWithPath:path];
if (URL) {
[ self setupDocumentControllerWithURL :URL];
// CGRect rect = CGRectMake ( 0 , 0 , kScreenW , kScreenH);
// [self.documentInteractionController presentOptionsMenuFromRect:rect inView:self.view animated:YES];//包含快速預(yù)覽菜單
[self.documentInteractionController presentPreviewAnimated:YES];
}
}
必須需要實(shí)現(xiàn)代理方法才能預(yù)覽
/**
* 預(yù)覽用的Controller
*/
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
/**
* 預(yù)覽用的View
*/
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
return self.view;
}
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
NSLog(@"will begin preview");
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
NSLog(@"did end preview");
}
這樣使用是可以了.當(dāng)你要在下載文件完成后打開文件,會(huì)這樣寫
NSData *data = responseObject;
BOOL isWrite = [data writeToFile:[MyTools filePathInTmpWithFile:fileName] atomically:YES];
MAIN(^{
[self.mbHud hideAnimated:YES];
});
if (isWrite) {//調(diào)用打開文件
[self openFileWithPath:[MyTools filePathInTmpWithFile:fileName]];
}else{
MAIN(^{
[MyTools showAlertWithTitle:kLocStr(@"提示") andContent:kLocStr(@"打開文件失敗!") andBlock:nil atController:self];
});
}
這樣會(huì)出現(xiàn)問題,當(dāng)下載的過程中,退出當(dāng)前頁面,下載會(huì)繼續(xù)的,下載完成后繼續(xù)運(yùn)行你的代碼打開文件進(jìn)行預(yù)覽,雖然你看不到這個(gè)過程,但是確實(shí)是這樣的,而且會(huì)造成當(dāng)前控制器無法釋放,造成內(nèi)存泄漏!
解決辦法:
@property (nonatomic ,assign) BOOL isAppear; //判斷是否當(dāng)前頁面正在顯示
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.isAppear = NO;
if (self.documentInteractionController) {
self.documentInteractionController.delegate = nil;
NSLog(@"\\ndelegate == nil\\n");
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.isAppear = YES;
if (self.documentInteractionController) {
self.documentInteractionController.delegate = self;
}
}
- ( void )setupDocumentControllerWithURL:( NSURL *)url
{
if (!self.isAppear) {
return;
}
if ( self.documentInteractionController == nil ){
self.documentInteractionController = [ UIDocumentInteractionController interactionControllerWithURL :url];
self.documentInteractionController.delegate = self ;
}
else {
self.documentInteractionController.URL = url;
}
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
NSLog(@"did end preview");
self.documentInteractionController = nil;
}