How do I read/write to files on Mobile?
This lesson looks at how to create a file and then read/write data to it. This has many practical applications from storing user settings between sessions to high score tracking. The sample stack we'll build together will be configured for the iPhone, but these principles will work for all the other mobile platforms - and indeed for desktop, if you want to write one block of code that works across all platforms.
You can download the sample stack from this url: https://tinyurl.com/ya4dfo2j
Create our basic stack
1) Create a new stack by selecting 'New Mainstack' from the file menu
2) Open the 'Stack Inspector' from the menubar
3) I've given my stack a name and also a title
4) Set the with and height of the stack to 320 * 460 (iPhone)
5) Prevent the stack from being resized
Our stack is now setup and we can move one to coding this example.
Decide where we want to write our file
Mobile deployment imposes strict controls over what you can and cannot access. Each application in mobile is stored in its own 'sandbox' folder (referred to as the home folder.) An application is free to read and write files within this folder and its descendants, but is not allowed to access anything outside of this.
When an application is installed on a phone (or in the simulator) a number of initial folders are created for use by the application. You can locate the paths to these folders using the specialFolderPath() function.
- home – the (unique) folder containing the application bundle and its associated data and folders.
- documents – the folder in which the application should store any document data (this folder is backed up by iTunes on sync)
- library – the folder in which to store data of various types (backed up by iTunes on sync). In particular, data private to the application should be stored in a folder named with the app's bundle identifier inside the "library" folder
- cache – the folder in which the application should store any transient data that needs to be preserved between launches (this folder is not backed up by iTunes on sync)
- temporary – the folder in which the application should store any temporary data that is not needed between launches (this folder is not backed up by iTunes on sync)
- engine – the folder containing the built standalone engine (i.e. the bundle). This is useful for constructing paths to resources that have been copied into the bundle at build time.
- resources - Same as "engine".
- documents – the folder where application-specific data can be placed (typically valuable)
- cache – the folder where transient application-specific data can be placed (typically not valuable)
- temporary – same as "cache"
- engine – the (virtual) folder containing the application's LiveCode engine and other resources that were copied into the application at build time
- resources – same as "engine".
- external documents – the folder on the primary shared/external storage device where application-specific data can be placed
- external cache – the folder on the primary shared/external storage device where transient application-specific data can be placed
- external temporary – same as "external cache"
Special Folder Path
You can test this entering the following line of LiveCode into the message box:
I am on a Mac so this will return the full path to my default Documents folder:
This will be different for every user. It will also change depending on your platform. LiveCode will return the default location for documents on whichever platform your application runs on.
Setting the folder using specialFolderPath
You can reference files in 1 of two ways:
1) Build the full path to your file every time you want to access it.
2) Set the folder that LiveCode performs all File and Folder operation within (default)
set the defaultFolder to specialFolderPath("documents")
Now, whenever you look to perform a Read or Write it will be done relative to the documents folder.
Setup our example
1) Drag on a new button. Name it "Write"
2) Drag on a second button. Name it "Read"
3) Drag on a label. Name it "write" and add some text to its contents
4) Drag on a label. Name it "read". I've added "No Data" to its contents just so that I can see it
Write our data to our file
Add the following script to the "Write" button:
on mouseUp set the defaultFolder to specialFolderPath("documents") put field "write" into URL ("file:test.txt") end mouseUp
There are various ways to read and write to files in LiveCode. You can 'Open' a file and then 'Read' or 'Write'. For smaller files I prefer to use the URL command. It allows us to simply 'Put' our data in the file. If the file you specified doesn't exist LiveCode will create it for you.
If you are writing binary data to your files your should use "binfile:" instead of "file:" before your filename.
Get the data back out of our file
Add the following script to your "Read" button:
on mouseUp set the defaultFolder to specialFolderPath("documents") put URL ("file:test.txt") into field "read" end mouseUp
Standalone Application Settings
When building a standalone that reads and write files there are some relevant settings.
iOS - The "File Sharing" requirement determines whether the Shared Files feature of iTunes is enabled for this application (UIFileSharingEnabled)
Android - Check the "Write external storage" application permission if your app will read or write files on external storage (e.g. an SD card)