0
votes

I have data.plist file on my ios Application I use it to store user preferences. I can read the file. but I can't write the file and I get this error on my catch.

notes: the error happens when I run the app on a test device

Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “data.plist” in the folder “MyApp”." UserInfo={NSFilePath=/private/var/containers/Bundle/Application/37FE297E-5801-4681-AA7C-6FCF9BAA9E87/MyApp.app/data.plist, NSUnderlyingError=0x283996880 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

func updateDataPlist(){

    self.thresholdForAdminBalances = Int(self.txtfThresholdForAdminBalances.text!)
    self.thresholdForSubUserbalances = Int(self.txtfThresholdForSubUserBalances.text!)

    let encoder = PropertyListEncoder()
    encoder.outputFormat = .xml

    let preferences =  Preferences(thresholdForAdminBalances: thresholdForAdminBalances!, thresholdForSubUserBalances: thresholdForSubUserbalances!)


    if let path = Bundle.main.url(forResource: "data", withExtension: "plist"){

        do {
            let data = try encoder.encode(preferences)
            try data.write(to: path)
        } catch {
            print(error)
        }
    }
}
1

1 Answers

1
votes

You don’t have permission to save the file “data.plist” in the folder “MyApp”

Your app's bundle is read-only. Save files in one of the writable directories, e.g. the document or the app support directory.

Use Filemanager.url(for:in:appropriateFor:create:) to get the URL for one of those directories.