Managing Files in iOS

Part 2/2: Pick files and Upload (Multipart)

Mansi Shah
3 min readJun 7, 2018

In my previous 📝 about Managing files, I had covered the Save, Share, Open, Preview, Copy, and Print actions on files; With this blog I aim to cover the picking of documents and uploading of files (which is generally Multipart form data).

If you are already familiar with the concept, feel free to skip this one and take a nap instead 😴💤

So let’s begin right away! 🤓

We will need to use the UIDocumentMenuViewController to pick documents from the device.

let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF), String(kUTTypeImage)], in: .import)importMenu.delegate = selfimportMenu.modalPresentationStyle = .formSheetpresent(importMenu, animated: true, completion: nil)

First things first, we decide what kind of documents do we want to allow our users to pick; Here I have chosen the type kUTTypePDF and kUTTypeImage which will allow all the documents with .pdf or all the image extensions (.jpeg, .png etc) to be pickable.

Next, we assign the delegate of our picker UIDocumentMenuViewController to `self` ( = your UIViewController from where you need to show the document picker).

Next, we assign the presentation style for our picker and then present the menu

The menu will have all the options of locations from where documents can be picked!

Now let’s work with the Delegate Methods👩‍💻

These methods are pretty self-explanatory!

From the didPickDocumentPicker method, we (well not us actually) then open whatever option was selected by the user from the above presented list (iCloud, Google Drive, Dropbox whateva..)

The documentPickerWasCancelled method, informs us that the document picker was cancelled (like if the cancel button was pressed in the image shown below)

Cancel Tap

Finally, the method didPickDocumentAt returns us the URL of the document that was picked from the picker! Bingo (This is the URL i.e local path of the document that was selected/picked by the user)

If you’re still wondering how to upload the document, I’ve got ya!

With this I conclude my File Managing with iOS, I’m feeling so happy 😌 with this conclusion, as I had spent a lot of time and hopped on many Stack overflow answers and tutorials for picking documents and saving them where my user could actually 👀

Sayonara

Pika Pika ☺️

More where this came from

This story is published in Noteworthy, where thousands come every day to learn about the people & ideas shaping the products we love.

Follow our publication to see more stories featured by the Journal team.

--

--