3

I am building a file picker using UIDocumentPickerViewController and I want to pick the following file types only (others needs to be disabled),

  • doc
  • docx
  • pdf
  • gdoc (Google doc file format)
  • txt
  • rtf

The code I have so far is as follows,

import UIKit
import MobileCoreServices

class DocumentPickerVC: UIViewController {

@IBAction func btnLocalTapped(sender: UIButton) {

    let types: [String] = [kUTTypeText as String, kUTTypePDF as String, "com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"]
    let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = .formSheet
    documentPicker.allowsMultipleSelection = true
    self.present(documentPicker, animated: true, completion: nil)
}

}

extension DocumentPickerVC: UIDocumentPickerDelegate {

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // you get from the urls parameter the urls from the files selected
    for url in urls {
        print(url)
    }
}

}

I can't seem to figure out what is the UTCoreType for Google docs.

I went through the list found here https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html, but no luck.

As a second attempt to find the UTCoreType I tried the following,

  1. Made all the files unlocked for the picker by changing the types as follows,

    let types = [kUTTypeItem as String]

  2. Downloaded file using the picker and moved it to the documents directory

  3. Enabled iTunes file sharing and made a copy to my desktop

  4. Ran the 'mdls' tool against the file

Command:

$ mdls doc2.gdoc

Output:

kMDItemContentCreationDate         = 2018-10-15 23:07:02 +0000
kMDItemContentCreationDate_Ranking = 2018-10-15 00:00:00 +0000
kMDItemContentModificationDate     = 2018-10-15 23:07:05 +0000
kMDItemContentType                 = "dyn.ah62d4rv4ge80s3dtqq"
kMDItemContentTypeTree             = (
    "dyn.ah62d4rv4ge80s3dtqq",
    "public.data",
    "public.item"
)
kMDItemDateAdded                   = 2018-10-15 23:07:12 +0000
kMDItemDateAdded_Ranking           = 2018-10-15 00:00:00 +0000
kMDItemDisplayName                 = "doc2.gdoc"
kMDItemFSContentChangeDate         = 2018-10-15 23:07:05 +0000
kMDItemFSCreationDate              = 2018-10-15 23:07:02 +0000
kMDItemFSCreatorCode               = ""
kMDItemFSFinderFlags               = 0
kMDItemFSHasCustomIcon             = (null)
kMDItemFSInvisible                 = 0
kMDItemFSIsExtensionHidden         = 0
kMDItemFSIsStationery              = (null)
kMDItemFSLabel                     = 0
kMDItemFSName                      = "doc2.gdoc"
kMDItemFSNodeCount                 = (null)
kMDItemFSOwnerGroupID              = 20
kMDItemFSOwnerUserID               = 501
kMDItemFSSize                      = 16848
kMDItemFSTypeCode                  = ""
kMDItemInterestingDate_Ranking     = 2018-10-15 00:00:00 +0000
kMDItemKind                        = "Document"
kMDItemLogicalSize                 = 16848
kMDItemPhysicalSize                = 20480
  1. Tried out the kMDItemContentType of the file but didn't work

Right now I'm out of ideas. Any thoughts?

ThE uSeFuL
  • 1,456
  • 1
  • 16
  • 29
  • I don't think Google has UTIs for their file types. Your best best is probably to declare a new one in your app as an imported UTI. – Thomas Deniau Oct 18 '18 at 12:27

0 Answers0