iOS, Titanium

Titanium: Reading Files in the Private Documents Directory

The 1.8 release of Titanium contains several iOS 5 compatibility updates.  One of the more interesting updates is the installation directory for database have changed.

In prior versions you could access the database like below:

function fetchDbFile(dbName){'We build the directory path to find ' + dbName + '.sql');
    return Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory, 'database', dbName + '.sql');

var myDbName = 'foo123';'We create a db to test our method')
var testDb =;'No we get the file')
var dbFile = fetchDbFile(myDbName);'Did we find it? ' + dbFile.exists());'Here is the nativePath ' + dbFile.nativePath);'Example Finished');

With iOS 5, Apple has introduced new guidelines that have altered the database installation directory.  Databases are now installed into the Private Documents Directory.  There currently is not a property for accessing this directory in the Ti.Filesystem module.

But since the Ti.Filesystem module only proxies the url request to iOS you can reference it directly. Below is a method that demonstrations how to do this.

function privateDocumentsDirectory(){'We need to open a file object to get our directory info');
    var testFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory);'Now we remove the Documents folder reference');
    var privateDir = testFile.nativePath.replace('Documents/','');'This is our base App Directory =' + privateDir);'Now we add the Private Documents Directory');
    privateDir+='Library/Private%20Documents/';'Our new path is ' + privateDir);
    return privateDir;

var myDbName = 'foo123';'We create a db to test our method')
var testDb =;
var testNativePath = testDb.getFile().nativePath;'Our nativePath to test against is ' + testNativePath + ' this is what we need to end up with');
var privateDocFolder = privateDocumentsDirectory();'Our Private Document Folde is ' + privateDocFolder);"Let's see if we can find our database");
var dbCheck = Ti.Filesystem.getFile(privateDocFolder, myDbName+ '.sql');'Did we find it? ' + dbCheck.exists());'Do our file paths match? ' + (dbCheck.nativePath==testNativePath));'Example Finished');

To help work with your existing databases Appcelerator has added a getFile property onto the database object.  This is extremely helpful if you are performing any back-up operations yourself.  See this link to read more.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s