iOS Document Picker

Santhosh Kumar
2 min readJun 5, 2018

--

We have a lot of Document based apps in a App Store. For example iCloud Drive,Google Drive and Dropbox.

In this tutorial I will explain about you how to sync data between document based apps to our apps in iOS 11.

  • Xcode
  • Membership in Apple Developer Program
  • Permission to enable CloudKit in your developer account
  • Provisioning Profile

The first thing you will a create Xcode project with out any build errors. Add provision profile and your Apple ID in Xcode Preference Accounts.

The next thing is Enable iCloud in your projects. Follow these steps :

Targets -> Capabilities-> Enable iCloud.(if you not added your account in Xcode Preferences you will get alert pop up error)

We have two types of container default and custom. In this project I have created a custom container.

For more about enable an iCloud refer link : https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/CloudKitQuickStart/EnablingiCloudandConfiguringCloudKit/EnablingiCloudandConfiguringCloudKit.html

If you completed those things almost successfully done a project. The next level is code.

Apple provides a UIDocumentViewController and UIDocumentMenuViewController to access document based apps. But unfortunately iOS 11 deprecated UIDocumentMenuViewController. So we can use UIDocumentViewController.

MobileCoreServices

Use uniform type identifier (UTI) information to create and manipulate data that can be exchanged between your app and other apps and services.

import MobileCoreServices

If you import core services then only you can able to do Document types. Document types are what ever you want is that in mobile core services.

let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText),String(kUTTypeContent),String(kUTTypeItem),String(kUTTypeData)], in: .import)

//Call Delegate

documentPicker.delegate = self

self.present(documentPicker, animated: true)

And finally confirms UIDocumentPickerDelegate in your class. Implement below delegate methods. The method is called once a document is selected.

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

print(urls)

}If you feel this tutorial is interesting please share it and clap it.

Originally published at medium.com on June 5, 2018.

--

--