UIDocumentPickerViewController允许在第一次运行应用程序时选择文件,但随后不能运行

时间:2018-08-19 12:36:24

标签: swift swift4.1 uidocumentpickerviewcontroller

我的应用程序在运行时会在其文档目录中创建一个json文档(使用UIDocument子类)。然后,在打开UIDocumentPickerViewController选择文件时,如果应用程序已写入新文件,则行为符合预期。

但是,如果我再次运行该应用程序(并覆盖上一个创建的文件),则除非调用几秒钟,否则不会调用委托方法didPickDocumentsAt。

我在这里想念什么?

@IBAction func showDocumentPicker() {
    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeJSON as String], in: .import)
    documentPicker.allowsMultipleSelection = false
    documentPicker.delegate = self
    self.present(documentPicker, animated: true, completion: nil)
} //this function is in the initial definition of the class and is connected to a UIBarButton


extension BudgetExportViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        print("selected document: \(urls.first)")
        print("555555555555555555555555555555")
        //document = BudgetExportDocument(fileURL: urls.first!)
//                CFURLStartAccessingSecurityScopedResource(urls.first! as CFURL)
//                let documentData = try? Data.init(contentsOf: urls.first!)
//                let json = try? JSONDecoder().decode(BudgetExportData.self, from: documentData!)
//                budgetThisMonth = json
//                print("Budgetthismonth")
//                print(budgetThisMonth)
//                CFURLStopAccessingSecurityScopedResource(urls.first! as CFURL)
    }
}

1 个答案:

答案 0 :(得分:0)

显然,问题出在我多次重写同一文件的事实。现在,如果存在同名文件,则不会覆盖该文件,并且UIDocumentPickerViewController行为符合预期。