iOS Swift,“我的iPhone”文件夹的URL是什么

问题描述 投票:0回答:1

我需要将一些已保存到“On My iPhone”文件夹中的文档导入我的应用程序。这个文件夹的URL是什么? (这些文档(pdf)由用户保存在应用程序外部,我希望尽可能将它们移动/复制到应用程序文档文件夹,但我再也找不到该URL。

ios swift url file-manager
1个回答
1
投票

试试这段代码(从iPhone文件夹中选择所需文件的方法:

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)
    }
}

YourViewController呼叫功能:

@IBAction func importPem(_ sender: UIButton) {

    initDocs()
    UIDocumentInteractionController().presentPreview(animated: true)
}
© www.soinside.com 2019 - 2024. All rights reserved.