How to open the Document files e.g(.pdf,.doc,.docx) in ios mobile when a button action using swift3.0?

后端 未结 3 1750
我在风中等你
我在风中等你 2020-12-05 05:15

I need to open UIDocumentPickerViewController and It should allow user to select all type of files. ie.(.pdf,.doc)files I used UIDocumentPickerViewControl

相关标签:
3条回答
  • 2020-12-05 05:48

    Just posting this answer to help other user for this.

    To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift: documentTypes would be

    import import MobileCoreServices

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

    Example:

    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)
    
    0 讨论(0)
  • 2020-12-05 06:01

    Swift 3*, 4*,

    To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in 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)
    
                   }
       }
    

    Note: If you intend to select all files the you have to use following code:

      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) 
    

    In your action method.

    0 讨论(0)
  • 2020-12-05 06:13

    Actually, instead of WebView you could also use the QuickLook Framework, which is designed for this specific purpose. You just pass the location of the file and conform to the protocols. It will present a view controller with the document inside.

    It supports the following file:

    - iWork documents (Pages, Numbers and Keynote)

    - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)

    - PDF files

    - Images

    - Text files

    - Rich-Text Format documents

    - Comma-Separated Value files (csv)

    Here is a tutorial by APPCODA, that describes the same.

    and

    Here is the tutorial provided by Apple OBJ-C version.

    0 讨论(0)
提交回复
热议问题