Search code examples
swiftgoogle-drive-apidropboxiclouduidocumentpickervc

DropBox Chooser and documentsPicker for Swift developers 3.0


While the Chooser implementations for iOS are present Here. It is however limited to Objective-C. Is it possible to create a chooser in swift manually?

(A dropBox chooser)

enter image description here

I am also unable to sufficiently call the documentspicker functions, where one can pull any document from any app the user may have installed.

enter image description here

Thank you in advance


Solution

  • Solved

    From your project's capabilites. First enable both the iCloud serivces and the key Sharing, now import MobileCoreServices in your class. Finally extended the following three classes inside your class.

    UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
    

    Now implement the following functions..

     public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
                  let myURL = url as URL
                  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 to call all of this? Add the following bit of code to your clickable 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 to your app. And the Url will be printed.

    enter image description here

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

    kUTTypePDF

    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.