Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

Implement Document Picker in swift (iOS)

I want to pick a file of any type(.pdf, .docs, .xlsx, .jpeg, .txt, .rtf, etc) functionality in my iOS app. On clicking on Upload button, I want my app to open a directory and select files(DocumentsPicker)

@IBAction pickDocument(sender: UIButton) {
    //Open Document Picker
}

Any approach to do so in Swift?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for iOS 14: You do not need any capabilities. Just create a UIDocumentPickerViewController with the appropriate types, implement the delegate, and you are done.

More info in this answer. Code from there:


import UIKit
import MobileCoreServices
import UniformTypeIdentifiers

func selectFiles() {
    let types = UTType.types(tag: "json", 
                             tagClass: UTTagClass.filenameExtension, 
                             conformingTo: nil)
    let documentPickerController = UIDocumentPickerViewController(
            forOpeningContentTypes: types)
    documentPickerController.delegate = self
    self.present(documentPickerController, animated: true, completion: nil)
}


From your project's capabilities, enable both the iCloud and the Key-Sharing.

enter image description here

Import MobileCoreServices in your class and then extend the following three classes inside your UIViewController:

UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate

Implement the following functions:

public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let myURL = urls.first else {
        return
    }
    print("import result : (myURL)")
}
      

public func documentMenu(_ documentMenu:UIDocumentMenuViewController, 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)
}

How do you call all of this? Add the following bit of code to your click function:

func clickFunction(){

let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .formSheet       
    self.present(importMenu, animated: true, completion: nil)
}

Click your button. The following menu will pop up ..

MenuPicker

In the case of Dropbox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.

enter image description here

Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.

kUTTypePDF kUTTypePNG kUTTypeJPEG ...

Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler

importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })

enter image description here

Enjoy it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...