Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative UIActivityViewController save dialog for Mac Catalyst or solution for UIDocumentPickerViewController throwing error code 260

I am looking for an alternative export menu other then UIActivityViewController for a Mac Catalyst app. While this works, the user can not choose where they want to save the file (the file is a JSON file of all the items in a list) and I would like the user to be able to choose the directory they want to save the JSON to. I have tried the following code but it gives the error "Error Domain=NSCocoaErrorDomain Code=260 'The file 'name.json' couldn’t be opened because there is no such file'" when you try to save a file.

The Code:

let fileManager = FileManager.default

do {
    let fileURL2 = fileManager.temporaryDirectory.appendingPathComponent("\(detailedList.lname!).json")

    // Write the data out into the file
    try jsonData.write(to: fileURL2)

    // Present the save controller. We've set it to `exportToService` in order
    // to export the data -- OLD COMMENT
    let controller = UIDocumentPickerViewController(url: fileURL2, in: UIDocumentPickerMode.exportToService)
    present(controller, animated: true) {
        // Once we're done, delete the temporary file
        try? fileManager.removeItem(at: fileURL2)
    }
} catch {
    print("error creating file")
}

I have tried Googling other ways or ways to get this to work but I cannot find anything that will work on Mac Catalyst. I know you can do this because I have seen other apps and examples do it but nothing works for me. So what would be a possible alternative way of doing this or a solution to this code?

like image 231
117MasterChief96 Avatar asked Nov 18 '25 03:11

117MasterChief96


1 Answers

The problem is that you are removing the file you wish to save before the user has a chance to choose where they want to save it.

The completion handler where you call try? fileManager.removeItem(at: fileURL2) is called as soon as the document picker is displayed.

The proper solution is to delete the file in the UIDocumentPickerDelegate methods, not when the picker is presented.

like image 99
rmaddy Avatar answered Nov 19 '25 16:11

rmaddy