Implement Document Picker in swift (iOS)

后端 未结 8 2186
滥情空心
滥情空心 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: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

提交回复
热议问题