当使用 swift3.0 进行按钮操作时,如何在 ios 移动设备中打开文档文件,例如(.pdf、.doc、.docx)?

问题描述 投票:0回答:4

我需要打开

UIDocumentPickerViewController
,它应该允许用户选择所有类型的文件。即(.pdf,.doc)文件我使用了
UIDocumentPickerViewController
方法。 我的代码:

UIDocumentPickerDelegate, UIDocumentMenuDelegate
在我的班级宣言中

//上传文件

func OpenFile(){

    let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .fullScreen
    self.present(importMenu, animated: true, completion: nil)

}
@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {        
    let cico = url as URL
    print("The Url is : \(cico)")

    do {
        let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
        print(weatherData)
        let activityItems = [weatherData]
        let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        if UI_USER_INTERFACE_IDIOM() == .phone {
            self.present(activityController, animated: true, completion: { _ in })
        }
        else {
            let popup = UIPopoverController(contentViewController: activityController)
            popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
        }

    } catch {
        print(error)
    }

    //optional, case PDF -> render
    //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)      


}

@available(iOS 8.0, *)
public func documentMenu(_ documentMenu:     UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

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


func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    print(" cancelled by user")

    dismiss(animated: true, completion: nil)        

}

在此代码中,应用程序将崩溃。原因是

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot initialize a UIDocumentPickerViewController except by the initWithDocumentTypes:inMode: and initWithURL:inMode: initializers.

我不知道如何初始化

initWithDocumentTypes:inMode.
我是iOS新手,有人可以帮助我吗??? 你能帮我吗..

ios swift3 document uidocument uidocumentmenuvc
4个回答
22
投票

斯威夫特 3*、4*、

要打开文档并选择任何文档,您可以使用

UIDocumentPickerViewController
,然后如果用户设备中连接了 Google Drive,则将显示 iCloud、文件和 Google Drive 中显示的所有文档。然后选定的文档需要在您的应用程序中下载,然后您可以在
WKWebView

中显示它
   @IBAction func uploadNewResumeAction(_ sender: Any) {

  /*  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"], in: .import) */

    let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

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

   extension YourViewController: UIDocumentPickerDelegate{


      func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

                let cico = url as URL
                print(cico)
                print(url)

                print(url.lastPathComponent)

                print(url.pathExtension)

               }
   }

注意:如果您打算选择所有文件,则必须使用以下代码:

  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"], in: .import) 

在你的行动方法中。


17
投票

在 Swift 中使用 UIDocumentPickerViewController 打开

(.pdf, .doc, .docx)
的特定文档文件: 文档类型将是

导入

import MobileCoreServices

["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]

示例:

let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
importMenu.delegate = self 
self.present(importMenu, animated: true, completion: nil)

15
投票

实际上,除了 WebView,您还可以使用 QuickLook Framework,就是为此特定目的而设计的。你只要通过 文件的位置并符合协议。它将呈现 一个包含文档的视图控制器。

它支持以下文件:

- iWork 文档(页面、数字和主题演讲)

- Microsoft Office 文档(只要它们是使用 Office 97 或任何其他较新版本创建的)

- PDF 文件

- 图片

- 文本文件

- 富文本格式文档

- 逗号分隔值文件 (csv)

这里是APPCODA的教程,描述相同。

这里是Apple OBJ-C版本提供的教程


1
投票

UIDocumentPickerViewController.init(documentTypes:in:)
kUTType___
分别在 iOS 14 和 15 中被弃用。以下是如何在较新版本的 iOS 上实现相同目标的方法:

let contentTypes: [UTType] = [
    .init(filenameExtension: "doc")!,
    .init(filenameExtension: "docx")!,
    .pdf
]

let documentPicker = UIDocumentPickerViewController.init(forOpeningContentTypes: contentTypes, asCopy: true)
documentPicker.delegate = self
present(documentPicker, animated: true)
© www.soinside.com 2019 - 2024. All rights reserved.