加入iTunes同步功能
- 导入QuickLook框架
- 在info中加入Application supports iTunes file sharing
预览并可选用第三方软件打开
控制器中创建UIDocumentInteractionController属性
/** 文件打开控制器 */
@property (nonatomic, strong) UIDocumentInteractionController * documentInteractionController;
遵守协议,设置当前控制器为代理
@interface OpenDocumentTools()<UIDocumentInteractionControllerDelegate>
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;
}