Julien7377
Julien7377

Reputation: 475

iOS Swift, what is the url of the "on my iPhone" folder

I need to import to my app some document that have been saved into the "On My iPhone" folder. What would be the url of this folder? (those document (pdf) are saved outside the app by the user and I would like if possible to move/copy them to the app document folder but again I can't find that url.

Upvotes: 0

Views: 708

Answers (1)

Agisight
Agisight

Reputation: 1818

Try this code (the way to pick needed document from iPhone Folder:

extension YourViewController: UIDocumentInteractionControllerDelegate {
    /// If presenting a top a navigation stack, provide the navigation controller in order to animate in a manner consistent with the rest of the platform
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self.navigationController ?? self
    }
}

extension YourViewController : UIDocumentPickerDelegate {
    func initDocs() {
        let pickerController = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)

        pickerController.delegate = self
        if #available(iOS 11.0, *) {
            pickerController.allowsMultipleSelection = false
        }
        present(pickerController, animated: false, completion: nil)
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if let url = urls.first {
            self.handleDoc(url: url) // method in your view controller
        }
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
        print("url = \(url)")
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        dismiss(animated: true, completion: nil)
    }
}

In YourViewController call function:

@IBAction func importPem(_ sender: UIButton) {

    initDocs()
    UIDocumentInteractionController().presentPreview(animated: true)
}

Upvotes: 1

Related Questions