I'm developing a loan related app where user upload his documents like pay-slips, it-returns, etc. For that I should show the user all the documents he/she having in his/her iPhone. How to show a picker for documents?
3 Answers
UIDocumentPickerViewController
is what you are looking for.
You init one with a list of document types you want to be able to pick, and a mode, which is usually .open
to get access to a file in a cloud provider directly. You can also use .import
which will copy the file to your container instead of giving you access to the file in the cloud provider's container directly, if the goal is just to upload it (you can remove the copy after uploading).
Once you have created your picker, you present it, and implement the delegate method didPickDocumentsAt
to retrieve the list of files chosen by the user.
Check out the Particles sample code and this years WWDC session « Managing Documents in your iOS Apps »

- 2,488
- 1
- 15
- 15
just call openDocumentPicker method when you want to upload document in your application..
import MobileCoreServices
func openDocumentPicker() {
let importMenu = UIDocumentMenuViewController(documentTypes: [kUTTypePDF as String], in: .import)
importMenu.delegate = self
self.present(importMenu, animated: true, completion: nil)
}
create your viewcontroller extension
extension ViewController: UIDocumentMenuDelegate {
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController) {
print("we cancelled")
dismiss(animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate {
internal func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: url.path)
let fileSizeNumber = fileAttributes[FileAttributeKey.size] as! NSNumber
let fileSizea = fileSizeNumber.doubleValue
let fileSize = fileSizea/1000000.0
if fileSize > 5.0 {
appDelegate.displayAlert(Title: "", Message: "Selected File are too big,Please select file less than 5.0 mb")
} else {
let documentData = try Data(contentsOf: url, options: .dataReadingMapped)
}
} catch let error {
print(error.localizedDescription)
}
}
}
above this code only you can access pdf if you want to access anpther document than just use this code
/*
let pdf = String(kUTTypePDF)
let spreadsheet = String(kUTTypeSpreadsheet)
let movie = String(kUTTypeMovie)
let aviMovie = String(kUTTypeAVIMovie)
let docs = String(kUTTypeCompositeContent)
let img = String(kUTTypeImage)
let png = String(kUTTypePNG)
let jpeg = String(kUTTypeJPEG)
let txt = String(kUTTypeText)
let zip = String(kUTTypeZipArchive)
let msg1 = String(kUTTypeEmailMessage)
let msg2 = String(kUTTypeMessage)
let types = [pdf, spreadsheet, movie, aviMovie, img, png, jpeg, txt, docs, zip, msg1, msg2]
*/

- 1,801
- 1
- 14
- 29
-
does it allow for all kinds of documents like doc, pdf, txt – Praveen Gowlikar Jun 18 '18 at 05:56
-
I'm working on your suggestion, i'll ping with result. – Praveen Gowlikar Jun 18 '18 at 05:57
-
in this code only pdf you can upload if you want another than tell me – Jigar Jun 18 '18 at 05:57
-
Got it i'll check out – Praveen Gowlikar Jun 18 '18 at 06:28
-
@PraveenGowlikar you got solution? – Jigar Jun 18 '18 at 06:42
-
Don’t use UIDocumentMenuViewController. It is deprecated. – Thomas Deniau Jun 18 '18 at 06:58
-
@ThomasDeniau i'm using UIDocumentPickerViewController – Praveen Gowlikar Jun 18 '18 at 07:01
-
if possible give me link @Thomas Deniau bcs i aleady used in my latest project – Jigar Jun 18 '18 at 07:01
-
@Thomas Deniau use this code in latest xcode tell me you got any warning or not. – Jigar Jun 18 '18 at 07:04
-
I do: https://developer.apple.com/documentation/uikit/uidocumentmenuviewcontroller says deprecated in iOS 11. In iOS 11, the document picker has been completely revamped. You don't need to present a menu first, and the menu won't list the right providers anyway. You just init a UIDocumentPickerViewController yourself and present it, you don't need to retrieve the picker from a menu view controller. – Thomas Deniau Jun 19 '18 at 08:56
iOS 11 or later
let importMenu = UIDocumentPickerViewController.init(documentTypes: ["public.item"], in: UIDocumentPickerMode.import)
self.present(importMenu, animated: true) {
}
Please Refer the following link for more description https://www.techotopia.com/index.php/An_iOS_Document_Browser_Tutorial
iOS 10 or earlier You can write the below function when your document picker opens. it will work for all the file types which you want to upload.
func openDocumentPicker() {
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.item"], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
-
Please do not use UIDocumentMenuViewController. It is deprecated. Just instantiate a UIDocumentPickerViewController directly yourself instead. – Thomas Deniau Jun 19 '18 at 08:58
-
@ThomasDeniau I have answered for the version of swift 3. it was not deprecated at that time. so I have written the code. btw thanks for the information – arpita Jun 19 '18 at 09:41
-
Sure ;) but it’s deprecated now so I felt this needed a newer answer for people searching and finding this question. – Thomas Deniau Jun 20 '18 at 07:40