Can you create, read, write a local file in iOS ?

Can you create, read, write a local file in iOS ?

Very new to all of this… can it be done ? Is it just the same as other console, desktop apps ? any examples ?

Thanks

Look at special folder, http://xojo.helpdocsonline.com/specialfolder$Documents

Absolutely. As Oliver says, use SpecialFolder to create a FolderItem in Documents:

Dim myFile As FolderItem = SpecialFolder.Documents.Child("MyFile.txt")

You then use that with Text/Binary streams and SQLite databases.

As an example, look at the Examples/iOS/Applications/XojoNotes which saves the notes data to a JSON file.

Absolutely, I have a couple of training videos for subscribers on how to do this at our Xojo training site http://xojo.bkeeney.com/XojoTraining/.

FWIW, there is also a new method in SpecialFolder that is helpful. The GetResource method will get a file in the Resources directory. That is exceptionally helpful.

In addition to Bob’s videos, the Hour of Code with IOS webinar I did this week shows how to create a Doodle app and save the drawing to a file so it can be reloaded when the app starts. The recording is now available:

Hour of Code with iOS

What about SQLite files? I have tried using SQLite stuff from the Eddies Electronics example in my own app with an existing database I have. It appears to be working when the app is running but when I stop and restart the app, the database changes are not really saved. Are there current SQLite limitations with IOS?

In the training video I’m reference I check to see if the SQLite file exists. If it does not, I create it using CreateDatabaseFile and an SQL script. If it does, I simply connect to it.

No magic here. This is what you do for desktop/web applications. Note that I’m doing this all via code - I refuse to use a db object created in the IDE - so if you’re used to doing it that way I apologize.

Robin, I see the same issue, the databasefile created by my app remains to excist in a debugging folder, in my case /users/jacco/Library/Developer/CoreSimulator/Devices/4AAAE96B-CAC1-4F05-98FA-BAF1EFFE7F4E/data/Containers/Data/Application/51AB673C-DB70-4891-8E00-D8AB13A73FC7/Documents

I used SpecialFolder.Documents to create and save the Sqlite database file. After connecting with the database, with SqlExecute I created a table and added 10 records. With SqlSelect I queried the table, that returned my 10 records. When I quit the App/Debugging session, the file remains, no data seems to be written to the file. Also there is no databaseschema present in the file. Next time I run the app it runs into databaseerrors because there are no tables present.

Are we forgetting something? Also trying db.Commit, db.ErrorMessage gives me errors that these are not valid members of SqliteDatabase

To do a commit you’ll have to do a db.SQLExecute(“COMMIT”). Or you could create your own extends Commit method that does the same thing.

You really should get used to doing transactions. It is the recommended way of doing things.

Jacco, This is all new to me but I got it figured out yesterday for me. I was not setting up the special folder properly but I finally got it going. This is my connect code.

Dim dbFile As FolderItem
dbFile = SpecialFolder.Documents.child(“BBDB.sqlite”)

If dbFile.Exists Then
App.BBDB = New SQLiteDatabase
App.BBDB.DatabaseFile = dbFile

If App.BBDB.Connect Then
  //LoadCustomers
End If

Else
AlertBox.Message = dbFile.Path
AlertBox.Title = “Unable to locate database.”
AlertBox.Show
End If

Everything gets auto-committed I believe.

My app copies the original database the first time it starts through a copy script in the build. I’ve tried it on the phone and it works great.

I didn’t get it figured out until I copied the app to the phone and each time I tried saving the app quit. I found out that the database being accessed was in the bundle and not in the documents folder because I was still using specialolder.getresource after

dbFile = SpecialFolder.Documents.child(“BBDB.sqlite”)

Hope that helps.

I was looking for the Commit, now it works fine. You are right Bob, transactions are the way to go.

Robin, I create my dbfile from code when the app starts, not from a copy script at build time. thanks for pointing out this option.

how can i save the data from my active app to the db.sqlite file

Create a new project, select Examples/iOS/Database/SQLiteExample. It shows how to a) create the db file, b) add a table, c) add data, d) update data.