美文网首页iOS常用
iOS App支持打开word,ppt,pdf文件

iOS App支持打开word,ppt,pdf文件

作者: 一亩三分甜 | 来源:发表于2018-11-25 22:18 被阅读0次

1.在info.plist文件中配置支持打开文件类型。

WX20181125-205010@2x.png
<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>com.cloud.OpenFile.common-data</string>
            <key>LSHandlerRank</key>
            <string>Default</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>
            </array>
        </dict>
    </array>

拷贝上面的代码到plist文件中保存后。


WX20181125-205431@2x.png

2.之后在AppDelegate文件中添加如下代码:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if (url != nil)
    {
        NSString *path = [url absoluteString];
        path = [path URLDecodedString:path];//必须转码,否则含有中文名字的文件无法打开
        NSMutableString *string = [[NSMutableString alloc] initWithString:path];
        if ([path hasPrefix:@"file://"])
        {
            [string replaceOccurrencesOfString:@"file://"withString:@""options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];//此句必需,且不能替换。
            //            [string  stringByReplacingOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
            [SaveFileManager sharedInstance].filePath = string;
            ViewController *vc = self.window.rootViewController;
            [vc openFile];
        }
    }
    return YES;
}

3.在根控制器中实现openFile方法,并遵循代理UIDocumentInteractionControllerDelegate,实现方法。

-(void)openFile{
    [self.documentVC dismissPreviewAnimated:YES];
    NSString *string = [SaveFileManager sharedInstance].filePath;
    NSURL *path=[NSURL fileURLWithPath:string];
    NSLog(@"打开时的路径:%@",[path absoluteString]);
    
    self.documentVC= [UIDocumentInteractionController interactionControllerWithURL:path];
    self.documentVC.delegate=self;
    if ([self.documentVC presentPreviewAnimated:YES])
    {
        NSLog(@"打开成功");
    }
    else
    {
        NSLog(@"打开失败");
    }
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
        return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller=
{
        return self.view; 
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller

{
    return self.view.frame;
    
}

效果如下:

1.gif

[下载Demo](https://github.com/yanyi0/OpenFile.git master)。

相关文章

网友评论

    本文标题:iOS App支持打开word,ppt,pdf文件

    本文链接:https://www.haomeiwen.com/subject/tijmqqtx.html