Implement Document Picker in swift (iOS)

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

提交回复
热议问题