从我的iOS应用程序中打开MS Word,Excel和PDF

时间:2019-07-03 14:54:46

标签: ios swift excel pdf ms-word

我有一个iOS应用程序,可以下载MS文档。下载文件后,将自动打开文件所在的位置并列出它们。这是我所知道的。我需要能够单击这些文件之一并将其打开,例如Files App。有人有没有关于如何从我的应用程序中打开MS文档的代码段?

我尝试了各种情况,但都失败了。

我有以下内容:

func OpenApp(){
    let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text", "com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document"], in: .import)

    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    let fileToOpen = url as URL

    //UIApplication.shared.open(fileToOpen, options: [:], completionHandler: nil)
    open(scheme: fileToOpen.absoluteString)
}   

func open(scheme: String) {
    if let url = URL(string: scheme) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:],
                                      completionHandler: {
                                        (success) in
                                        print("Open 10+ \(scheme): \(success)")
            })
        } else {
            let success = UIApplication.shared.openURL(url)
            print("Open \(scheme): \(success)")
        }
    }
}

该文件永远不会打开,并且在“打开”功能中显示为FALSE(成功)。一切正常,直到单击列出的文件将其打开。

我还将它添加到了info.plist

info.plist image

0 个答案:

没有答案