Implement Document Picker in swift (iOS)

后端 未结 8 2135
滥情空心
滥情空心 2020-11-30 19:54

I want to pick a file of any type(.pdf, .docs, .xlsx, .jpeg, .txt, .rtf, etc) functionality in my iOS app. On clicking on Upload button, I want my app to op

相关标签:
8条回答
  • 2020-11-30 20:04

    this will help you to implement download/upload functionality

    UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeImport | UIDocumentPickerModeExportToService];
    

    For more read Apple Documentation

    0 讨论(0)
  • 2020-11-30 20:11

    Something I struggled with was how to specify some specific formats for the PickerView, such as .pptx & .xlsx files. Here's some code to create a PickerView with some commonly required types...

    let types: [String] = [
        kUTTypeJPEG as String,
        kUTTypePNG as String,
        "com.microsoft.word.doc",
        "org.openxmlformats.wordprocessingml.document",
        kUTTypeRTF as String,
        "com.microsoft.powerpoint.​ppt",
        "org.openxmlformats.presentationml.presentation",
        kUTTypePlainText as String,
        "com.microsoft.excel.xls",
        "org.openxmlformats.spreadsheetml.sheet",
        kUTTypePDF as String,
        kUTTypeMP3 as String
    ]
    let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = .formSheet
    self.present(documentPicker, animated: true, completion: nil)
    

    There are two places that I found useful in putting together this list:

    https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

    https://escapetech.eu/manuals/qdrop/uti.html

    Hope that helps somebody!

    0 讨论(0)
  • 2020-11-30 20:17

    The UIDocumentMenuViewController is deprecated since iOS11. I also found it buggy when presented from a modal view controller. Here's a direct way of using the picker:

    import MobileCoreServices
    
    private func attachDocument() {
        let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet]
        let importMenu = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
    
        if #available(iOS 11.0, *) {
            importMenu.allowsMultipleSelection = true
        }
    
        importMenu.delegate = self
        importMenu.modalPresentationStyle = .formSheet
    
        present(importMenu, animated: true)
    }
    
    extension AViewController: UIDocumentPickerDelegate, UINavigationControllerDelegate {
        func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
            viewModel.attachDocuments(at: urls)
        }
    
         func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
            controller.dismiss(animated: true, completion: nil)
        }
    }
    

    As usual don't forget to add iCloud support:

    0 讨论(0)
  • 2020-11-30 20:25

    From your project's capabilities, enable both the iCloud and the Key-Sharing.

    Import MobileCoreServices in your class and then extend the following three classes inside your UIViewController:

    UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
    

    Implement the following functions:

    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let myURL = urls.first else {
            return
        }
        print("import result : \(myURL)")
    }
    
    
    public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }
    
    
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("view was cancelled")
        dismiss(animated: true, completion: nil)
    }
    

    How do you call all of this? Add the following bit of code to your click function:

    func clickFunction(){
    
    let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
        importMenu.delegate = self
        importMenu.modalPresentationStyle = .formSheet       
        self.present(importMenu, animated: true, completion: nil)
    }
    

    Click your button. The following menu will pop up ..

    In the case of Dropbox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.

    Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.

    kUTTypePDF kUTTypePNG kUTTypeJPEG ...

    Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler

    importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
    

    Enjoy it.

    0 讨论(0)
  • 2020-11-30 20:25

    You can use UIDocumentPickerViewController to get the files from the Files apps or iCloud Drive.

    1. Activate the Key-value storage and iCloud Documents from iCloud capability:

    1. Import the following framework on the view controller you want to open the document picker:

      import MobileCoreServices
      
    2. Implement the following method from UIDocumentPickerDelegate:

      func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
           // you get from the urls parameter the urls from the files selected
      }
      
    3. Create an UIDocumentPickerViewController to display the File picker or iCloud Drive:

      let types: [String] = [kUTTypePDF as String]
      let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import)
      documentPicker.delegate = self
      documentPicker.modalPresentationStyle = .formSheet
      self.present(documentPicker, animated: true, completion: nil)
      

      If you want the user can import more types of files to your app, you have to add more UTTypes to the types NSArray. To see all the types available, you can check the UTType Docs

    When the document picker opens on iOS 11 and you try to select a file inside the Google Drive, it is possible the file is disable due a bug: http://www.openradar.me/24187873

    0 讨论(0)
  • 2020-11-30 20:30

    iCloud access all type of files #

    func openiCloudDocuments(){
        let importMenu = UIDocumentPickerViewController(documentTypes: [String("public.data")], in: .import)
        importMenu.delegate = self
        importMenu.modalPresentationStyle = .formSheet
        self.present(importMenu, animated: true, completion: nil)
    }
    
    0 讨论(0)
提交回复
热议问题