No callback while using UIDocumentPickerViewController

1.6k views Asked by At

I'm using UIDocumentPickerViewController to select a document from the iPhone.

I have implemented the feature in the following way.

class Si3EntityDocumentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIDocumentInteractionControllerDelegate, UIDocumentPickerDelegate, UINavigationControllerDelegate, UIDocumentMenuDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setLoader()
        getDocuments(id:entity.id)
        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
        button.backgroundColor = .red
        button.setTitle("Upload Doc", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        self.view.addSubview(button)
        // Do any additional setup after loading the view.
    }

    @objc func buttonAction(sender: UIButton!){
        let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText),String(kUTTypeContent),String(kUTTypeItem),String(kUTTypeData)], in: .import)
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: {
            documentPicker.delegate = self
        } )
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        print(urls)
    }

}

The func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt url: URL) method has been deprecated in iOS 11 so i have replaced it with func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])

For some reason, I'm not getting the callback in func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])

I also have some other code in this class but I've not included it. When I select a document or click cancel I get the following error in the console

[DocumentManager] The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}

I know I'm missing out something very silly, any help is appreciated.

Thanks in anticipation.

3

There are 3 answers

0
sukh.ios On

I Used this Methods in swift 3

func openImportDocumentPicker() {
    let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = .formSheet
    self.present(documentPicker, animated: true, completion: { _ in })
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    if controller.documentPickerMode == .import {
        let alertMessage: String = "Successfully imported \(url.absoluteURL)"
   }
}

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
    print("Cancelled")
}
4
Prashant On

You can have a look at the Apple documentation for a better understanding.

The default behaviour of a UIDocumentPickerViewController is to pick one document and you would have to use:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
}

If you are using the UIDocumentPickerViewController to pick multiple documents, then you will have to set the allowsMultipleSelection property to true and implement

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
}

Also, the following line would suffice.

present(documentPicker, animated: true)
0
Luke On

Try on a device. Simulator wasn't working for me when I logged into iCloud and tried to view a directory.