'Json file in Kivy-iOS can't be updated and gives Operation not permitted[errno 1] in Xcode

I am in final phase of publish a nice game in App Store. However, I am stuck with a problem. In my app there are json files which saves and loads game in every step. They all works fine in Google Play Store.

I have: macOS Big Sur 11.6

Python 3.10.4

XCode Version 13.2.1

iPhone 7 iOS 14.7.1

I have build the Xcode file with kivy-ios toolchain.

I use those codes to read and write json files;

#TO READ THE DATA FROM kel1.json:  !!WORKS FINE!!
file = open('kel1.json',"r",encoding='utf-8')  
klist = json.load(file) 

#TO CLEAN ALL THE DATA IN savelist1.json: !!GIVES ERROR!!
telal={}
json_objecti = json.dumps(telal) 
with open("savelist2.json", "w") as outfile:
    outfile.write(json_objecti).   


#TO APPEND A DATA IN savelist1.json:  !!GIVES ERROR!!
dict={aln:"item"}
file=open("savelist1.json", "r+",encoding='utf-8')
data = json.load(file)
data.update(dict)
file.seek(0)
json.dump(data, file,ensure_ascii=False) 

as I said all above codes work fine when I turn that app to APK, AAB and after I publish the AAB to the GooglePlay.

Also when I run simulation in Xcode, it works fine as well. But when I connect my phone and run the app, "r" json works but "r+" and "w" gives error;

   File "/Users/batuhan/programs/kivy-ios/2800-ios/YourApp/main.py", line 429, in secom
 PermissionError: [Errno 1] Operation not permitted: '/private/var/containers/Bundle/Application/C2508051-03DA-40EC-8587-A40D8B922055/2800.app/YourApp/savelist1.json'
2022-05-19 20:14:09.322995+0300 2800[3486:1776834] Application quit abnormally!
2022-05-19 20:14:09.416628+0300 2800[3486:1776834] Leaving

I have found those and try;

How can I allow permissions for my Kivy app to access text files when running on an iOS device?

Permission denied to .json file when using a JSON file as a store in Kivy app

Python-Kivy on iOS (Xcode): Got dlopen error on Foundation: (...): image not found

Kivy: Error on iOS using JSON file for High Score

What have I did so far;

I gave all the permissions in security and privacy in 'full disk access', 'accessibility' and 'files and folders' for Xcode, python, terminal, finder.

I manually give read&write privilege for every folder, with every item inside them on folder info.

I have tried to JSONStore but because of encoding problem I have dropped it.

I have tried;

path1=os.path.dirname(os.path.abspath(__file__))
tela={}
json_object = json.dumps(tela, indent = 4)
file_path1=os.path.join(path1,"savelist1.json")
with open(file_path1, "w") as outfile:
    outfile.write(json_object) 

#THIS ONE WORKS FINE IN SIMULATOR BUT GIVES SAME ERROR IN PHONE AS WELL

I have tried;

tela={}
json_object = json.dumps(tela, indent = 4)
script_dir=os.path.dirname(__file__)
file_path1=os.path.join(script_dir, "savelist1.json")
with open(file_path1, "w") as outfile:
    outfile.write(json_object)

#THIS ONE WORKS FINE IN SIMULATOR BUT GIVES SAME ERROR IN PHONE AS WELL

I have cleaned and build folder in Xcode at every step.

I have tried user_data_dir, but it returns the path to ~/Documents/<app_name> and json file is not there. Json files are in same folder with main.py and my.kv.

I still can't find the right method to update json file in iPhone.

Please help.



Solution 1:[1]

it is necessary to define the file location as specified here(https://kivy.org/doc/stable/api-kivy.app.html?highlight=user_data_dir#kivy.app.App.user_data_dir)

Solution 2:[2]

It seems that I changed the port number from 8077 to 80, then it runs well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lion Real
Solution 2 Farwell_Liu