iOS Open Document picker in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import UIKit class DocumentPickerViewController: UIViewController { @IBOutlet weak var fileDetails: UITextView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func openDocument(_ sender: Any) { selectDocument() } } extension DocumentPickerViewController: UIDocumentPickerDelegate{ fileprivate func selectDocument(){ // kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet // String(kUTTypePDF),String(kUTTypeRTFD),String(kUTTypeText),String(kUTTypeSpreadsheet) // let test = UIDocumentPickerViewController let importMenu = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import) importMenu.delegate = self importMenu.modalPresentationStyle = .formSheet self.present(importMenu, animated: true, completion: nil) } public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { let fileUrl = url as URL print("import result : \(fileUrl)") let theFileName = (fileUrl.absoluteString as NSString).lastPathComponent print(theFileName) fileDetails.text = fileUrl.absoluteString fileDetails.insertText("\n\n") fileDetails.insertText(theFileName) // Create a FileManager instance } public func documentMenu(_ documentMenu:UIDocumentPickerViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) { documentPicker.delegate = self present(documentPicker, animated: true, completion: nil) } func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { print("view was cancelled") dismiss(animated: true, completion: nil) } } |
Download Project