Search code examples
iosxcodepython-2.7kivy

Can't write file on iOS using kivy in python


I'm writing an app and attempting to use the mapview module from kivy's garden library, and everything works fine until the downloader module from mapview tries to write the map tile it downloads. It spits out: Downloader error: IOError(1, 'Operation not permitted') And the file that it is trying to write is: 'cache/26a7511794_11_1041_1360.png' which would be in the cache folder of the app's directory (I assume). I've also tried changing the cache folder (as specified in the CACHE_DIR variable of the __init__.py module from mapview) from cache to /Library/Caches (as done in https://github.com/kivy-garden/garden.mapview/issues/28) but I get a permission error there as well.

What is the correct string for the CACHE_DIR folder to make mapview work on iOS? Or perhaps there's something in Xcode I need to set for this to work?


Solution

  • I followed a great tip from Albert Gao at his post here to solve this issue. I had to make a VERY SIMPLE modification to the mapview module's __init__.py script. The default folder for caches wouldn't work on ios. Here is the code in __init__.py before:

    CACHE_DIR = "cache"
    

    here is the code after fixing it to work on iOS:

    from kivy.utils import platform
    from kivy.app import App
    import os.path
    if platform == 'ios': # Erik Sandberg fix 10/14/2018
        root_folder = App().user_data_dir
        CACHE_DIR = os.path.join(root_folder, 'cache')
        cache_folder = os.path.join(root_folder, 'cache')
        CACHE_DIR = cache_folder
        #CACHE_DIR = "Library/Caches"
    else:
        CACHE_DIR = "cache"
    

    Reasoning: the default folder where it tries to make the "cache" directory is not actually where the main app is running. The main app is truly running from the path gotten from App().user_data_dir. iOS would not let me create files in a folder that was not below where the main app was truly running from.

    I hope that helps anyone else running into a similar problem!