-1

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Praveen Gowlikar
  • 215
  • 2
  • 16

3 Answers3

1

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 »

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
0

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]
 */
Jigar
  • 1,801
  • 1
  • 14
  • 29
0

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)
    }
Community
  • 1
  • 1
arpita
  • 175
  • 9
  • 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