0

我的应用程序在运行时会在其文档目录中创建一个 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)
    }
}
4

1 回答 1

1

显然,问题在于我多次覆盖同一个文件。现在,如果存在同名文件,它不会覆盖它,并且 UIDocumentPickerViewController 的行为符合预期。

于 2018-08-19T12:52:24.487 回答