Implement Document Picker in swift (iOS)

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

提交回复
热议问题