gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:14:00 60 4
gpt4 key购买 nike

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

但是,如果我再次运行该应用程序(并覆盖上次创建的文件),则不会调用委托(delegate)方法 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)
}
}

最佳答案

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

关于swift - UIDocumentPickerViewController 允许在应用程序第一次运行时选择文件,但随后不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51917726/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com