两种方式预览和打开文件
方式一:UIDocumentInteractionController
1、预览文件
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:strUrl]];
[self.documentController setDelegate:self];
self.documentController.UTI = @"com.microsoft.word.doc";
[self.documentController presentPreviewAnimated:YES];```
####2、打开文件
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:strUrl]];
self.documentController.delegate = self;
// self.documentController.UTI = @"com.adobe.pdf";
self.documentController.UTI = @"com.microsoft.word.doc";
BOOL canOpen = [self.documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
if (!canOpen) {
[LVAlert lvAlertWithTitle:@"没有程序可以打开要分享的文件" Message:nil Default:@"确定"];
}```
3、需要的代理方法
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController{
return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
return self.view.bounds;
}
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
NSLog(@"开始预览");
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
NSLog(@"结束预览");
}
方式一:QLPreviewController
1、预览文件
#import "QLPreviewController+BlocksKit.h"//头文件
<QLPreviewControllerDataSource, QLPreviewControllerDelegate>代理
#pragma mark -- PreviewDelegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *strUrl = [[DBManager sharedManager] selectContractModelWithType:@1 andID:self.orderModel.ItemID];
return [NSURL fileURLWithPath:strUrl];
}
//创建QLPreviewController
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
myQlPreViewController.delegate =self;
myQlPreViewController.dataSource =self;
[myQlPreViewController setCurrentPreviewItemIndex:0];
[self presentViewController:myQlPreViewController animated:YES completion:nil];