LiveCode LessonsHow To - LiveCode Mobile Tasks iOS TasksHow do I read/write to files on Mobile?

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.

Attached Files

Create our basic stack

Create our basic stack

1) Create a new stack by selecting 'New Mainstack' from the file menu

2) Open the 'Stack Inspector' from the object menu

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.

homethe (unique) folder containing the application bundle and its associated data and folders.

documentsthe folder in which the application should store any document data (this folder is backed up by iTunes on sync)

cachethe 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)

temporarythe 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)

enginethe 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.

Special Folder Path

You can test this entering the following line of LiveCode into the message box:

put specialFolderPath("documents")

I am on a Mac so this will return the full path to my default Documents folder:

/Users/ben/Documents

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

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

10 Comments

Ezra Weinstein

Please see my forum post if you are trying to copy a file from your application folder to another folder for write purposes (in my case I was shipping a database file that I had to move from the app folder which is read only to the Documents folder on an Android device).

http://forums.runrev.com/viewtopic.php?f=53&t=7669

Mike

How do you list the file names in the documents folder?

Elanor Buchanan

Hi Mike

You can use the function "the files" to list the documents in the defaultFolder.

I hope that helps.

Elanor

Seppo Hietanen

Is it wise to try to change the old Mac rev- stacks for iPad, or rewrite them?

Tbill

For the iPad - I need to create a series of tables with relationships between these tables. How is this done and how can I ensure that these tables are backed up using the 'iCloud'.

Hanson Schmidt-Cornelius

Hi Tbil,

you could use SQLite to create a database on your iPad. Look at this lesson for further information on the topic: http://lessons.runrev.com/s/lessons/m/4069/l/30516-how-to-create-and-use-an-sqlite-database.
If you write files to the "documents" path, then the content is automatically backed up, as long as the user has set permissions for this to happen. Have a look at the dictionary entry on "specialFolderPath" for more information on this.

Kind Regards,

Hanson

Ty

I can use the specialfolderpath when designing the app on my mac, but when I run the simulator or move it to my phone, it can find the file. Do I use "documents" or home for writeable file with a Iphone app?
Thank you

Hanson Schmidt-Cornelius

Hi Ty,

it depends for what you want to use the files you are saving on your iOS device. If they are only temporary files that are used during the execution of the application, then use the "temporary" path.
If the files are to remain accessible for each launch of the application, then use the "documents" path. This is where application specific information is stored that is also backed up by iTunes.

Kind Regards,

Hanson

Martin Meili

Hi there,
Is it also possible to create a specific folder within the documents folder? No problem on the osx side, but does this also work on the iOs side?
Cheers Martin

Hanson Schmidt-Cornelius

Hi Martin,

sure, you can create folders within the documents folder. Have a look at the dictionary entry for: "create folder".

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment