Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting document extensions in UIDocumentPicker in swift

Tags:

ios

uikit

swift

I am using document picker for picking documents and upload on Server. All I need to pass some extension for the allowed documents, But I am not able to find out what extension represents to what type of document. Here is a list of extensions I want to allow. - docx - Microsoft Word - xlsx - Microsoft Excel - pptx - Microsoft Powerpoint - pdf - csv - pages - numbers - key - Keynote - rtf - txt i know some extensions like "kUTTypePDF,kUTTypeRTF", but not all please help.

like image 557
Inderjeet Singh Avatar asked Aug 29 '18 06:08

Inderjeet Singh


Video Answer


3 Answers

Swift 5.0

let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet] // You can add more types here as pr your expectation
let importMenu = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)

Try this one

If you want to select all files then 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.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"], in: .import)
like image 120
Nikunj Kumbhani Avatar answered Oct 19 '22 03:10

Nikunj Kumbhani


Apple has posted a list of the built-in document types here:

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

I believe that should have all the ones you are looking for. If not, then you'll have to add your own types. You can do this by selecting the Project in Xcode and going into the Info tab. Then at the bottom, add a new Imported UTI for each file type.

  • Description: User-friendly description
  • Identifier: This is the identifier you will use in the documentTypes parameter for UIDocumentPickerViewController. com.yourcompany.yourproduct.fileextension or similar.
  • Conforms to: "public.data, public.content"

Then under Additional Imported UTI properties click to add the following:

UTTypeTagSpecification (Dictionary)
    public.filename-extension (Array)
        Item 0 (String): the file extension without "." (i.e. "csv")
like image 29
mikepj Avatar answered Oct 19 '22 03:10

mikepj


For .doc, .docx, you can use "com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"

For .xlsx, you can use "org.openxmlformats.spreadsheetml.sheet"

For .xls, you can use "com.microsoft.excel.xls"

like image 4
Tà Truhoada Avatar answered Oct 19 '22 02:10

Tà Truhoada