Tudor Croitoru Domi
Tudor Croitoru Domi

Reputation: 63

UIDocumentPickerViewController allows selecting a file the first time the app is run, but not subsequently

My app, when run, creates a json document (using a UIDocument subclass) in its document directory. And then, when opening up the UIDocumentPickerViewController to select a file, if the app has written a new file, the behaviour is as expected.

However, if I run the app again (and overwrite the last created file), the delegate method didPickDocumentsAt doesn't get called, unless I browse around for a few seconds.

What am I missing here?

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

Upvotes: 0

Views: 2299

Answers (1)

Tudor Croitoru Domi
Tudor Croitoru Domi

Reputation: 63

Apparently, the problem was the fact that I was overwriting the same file so many times. Now, if a file with the same name exists, it won't write over it and the UIDocumentPickerViewController behaviour is as expected.

Upvotes: 1

Related Questions