预览文档,使用第三方软件打开文档

加入iTunes同步功能

  • 导入QuickLook框架
导入QuickLook框架
  • 在info中加入Application supports iTunes file sharing
预览文档,使用第三方软件打开文档_第1张图片
屏幕快照 2016-06-29 下午2.47.10.png

预览并可选用第三方软件打开

控制器中创建UIDocumentInteractionController属性

/** 文件打开控制器 */
@property (nonatomic, strong) UIDocumentInteractionController * documentInteractionController;

遵守协议,设置当前控制器为代理

@interface OpenDocumentTools()

self.documentInteractionController.delegate = self;

创建预览窗口,并可选择第三方软件打开

- (void)openDocumentWithName:(NSString *)name {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",name]];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    if (url) {
        self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
        
        [self.documentInteractionController setDelegate:self];
        
        [self.documentInteractionController presentPreviewAnimated:YES];
    } else {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"打开失败" message:@"打开文档失败,可能文档损坏,请重试" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
#pragma mark - delegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    return self;
}

你可能感兴趣的:(预览文档,使用第三方软件打开文档)