利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档

1.添加

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"];


3.

    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;

}

6.

    [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(@"打开失败");

        }

    }










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值