Unable to write file with Data.write()

On a real iOS device, I am unable to write data to overwrite a file in my bundle. I am getting an error You don't have permission to save the file "CU_config.js" in folder "2023". Here is the code that tries to write the file.

func configure() {
        let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.javaScript], asCopy: true)
        picker.delegate = self
        present(picker, animated: true)

    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        controller.dismiss(animated: true, completion: nil)
        
        urls.forEach { url in
            let sourceURL:URL
            if(isMatch){
                sourceURL = Bundle.main.bundleURL.appendingPathComponent("www/2023/CU_config").appendingPathExtension("js")
            }else{
                sourceURL = Bundle.main.bundleURL.appendingPathComponent("www/2023/CU_Pit_config").appendingPathExtension("js")

            }
            let destURL = url

            let incoming:Data?
            do {
                incoming = try Data(contentsOf: destURL)
            }catch{
                incoming = nil
                print("Unable to find file")
                let alert = UIAlertController(title: "Error", message: "Unable to find file", preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
            
            do {
                sourceURL.startAccessingSecurityScopedResource()
                try incoming!.write(to: sourceURL)
                sourceURL.stopAccessingSecurityScopedResource()
            }catch{
                let alert = UIAlertController(title: "Error", message: "Unable to write file"+error.localizedDescription, preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
                print("Unable to write file")
            }
            webview.reload()
        }
    }

I tried giving it permission in the info.plist by using "Application supports iTunes file sharing" but that didn't work.

It works perfectly in simulation.

This is expected. You cannot write to the app bundle, it is read-only.

Your mutable data needs to go elsewhere in the filesystem.

There ought to be some introductory docs about this somewhere….

There ought to be some introductory docs about this somewhere

Yep. I looked for this specific guidance when working on “Embedding nonstandard code structures in a bundle” and eventually ended up adding a Separate read-only and read/write content section.

As to where one should store ones files, I don’t think we have a good overview of that )-:

Anyway, AnkurR, the specific API you’re looking for here is the url(for:in:appropriateFor:create:) method on FileManager.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm trying to overwrite a file that is being used by an HTML web-view. Is there an easy way for that HTML file to reference the js file which is outside of the bundle? Do I need to move all of the HTML files out of the bundle?

Earlier I wrote:

As to where one should store ones files, I don’t think we have a good overview of that )-:

The only doc I could find on this topic was the File System Programming Guide in the Documentation Archive, so I filed a bug requesting an updated doc (r. 119248146).


I'm trying to overwrite a file that is being used by an HTML web-view.

Ah, that’s tricky. To be specific, this is WKWebView, right?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Unable to write file with Data.write()
 
 
Q