UIDocumentInteractionController *documentInteraction;
<UIDocumentInteractionControllerDelegate>
2.下载的文件路径:
NSArray*filePaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString*filePath1=[filePaths objectAtIndex:0];
filePath1=[filePath1 stringByAppendingPathComponent:@"zhengwen.doc"];
NSLog(@"%@",filePath1);
本地的文件路径:
NSString*filePath2=[[NSBundlemainBundle]pathForResource:@"zhengwen"ofType:@"doc"];
NSURL *path=[NSURLfileURLWithPath:filePath2];//我这儿是本地的文件
documentInteraction= [UIDocumentInteractionController
interactionControllerWithURL:path];
documentInteraction.delegate=self;
if ([documentInteractionpresentPreviewAnimated:YES]){
NSLog(@"打开成功");
}
else{
NSLog(@"打开失败");
}
4.添加代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
returnself;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
returnself.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
到此就实现了文档预览的功能!假如想要编辑文件里的内容怎么办?调起第三方的app软件例如:Word
可是编辑完了还要回传给自己的APP!需要在plist文件里添加
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>自己的Bundle Identifier为什么我也不知道</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.text</string>
<string>public.data</string>
<string>public.docx</string>
<string>public.doc</string>
</array>
</dict>
</array>
5.在AppDelegate中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnullNSDictionary<NSString *,id> *)options
{
if (url !=nil) {
NSString *path = [urlabsoluteString];
NSMutableString *string = [[NSMutableStringalloc] initWithString:path];
if ([pathhasPrefix:@"file://"])
{
[string replaceOccurrencesOfString:@"file://"withString:@""options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];//这儿是个坑
[singletonShareDataBase].FileUrl=string;//单例保存返回给的文件的地址
}
}
returnYES;
}
[documentInteractiondismissPreviewAnimated:YES];//关闭以前documentInteraction
//新起一个,然后根据传回来的文件地址打开文件!展示出来
NSURL *path=[NSURLfileURLWithPath:[singletonShareDataBase].FileUrl];
documentInteraction= [UIDocumentInteractionController
interactionControllerWithURL:path];
documentInteraction.delegate=self;
if ([documentInteractionpresentPreviewAnimated:YES]){
NSLog(@"打开成功");
}
else{
NSLog(@"打开失败");
}
IOS 11以后明明可以打开成功!但是却打不开文件!提示如下!
2018-02-01 11:35:27.973526+0800 PDF[7974:3163464] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-02-01 11:35:27.974910+0800 PDF[7974:3163464] [MC] Reading from public effective user settings.
2018-02-01 11:35:28.108842+0800 PDF[7974:3163464] [default] Couldn't issue file extension for url: file:///var/containers/Bundle/Application/D076C23C-37EA-4012-8E9A-9DC2B98115B8/PDF.app/%E9%B1%BC%E5%84%BF%E9%A3%9E.pdf #PreviewItem
后来查到IOS11以后!PDF换成了PDFKit.framework!加入系统
#import <PDFKit/PDFKit.h>
NSURL *path = [[NSBundle mainBundle] URLForResource:@"鱼儿飞" withExtension:@"pdf"];
if (@available(iOS 11.0, *)) {
PDFView *pdfView = [[PDFView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-40)];
PDFDocument *Document = [[PDFDocument alloc]initWithURL:path];
Document.delegate = self;
pdfView.document = Document;
[self.view addSubview:pdfView];
} else {
_documentInteraction= [UIDocumentInteractionController
interactionControllerWithURL:path];
_documentInteraction.delegate=self;
if ([_documentInteraction presentPreviewAnimated:YES]){
NSLog(@"打开成功");
}
else{
NSLog(@"打开失败");
}
}