Changes to filesystem on iOS

information superhighway

With the release of Titanium 1.7.0, there have been some changes put in place on iOS to help with relative and absolute path resolution for files on device, and to allow better cross-platform development by interpreting paths and providing directory information the same way that Titanium on Android does.

Previously, file operations on iOS were performed only on paths, which were used interchangably with file URIs. There was always some confusion about where exactly a “relative” path was relative to, and whether absolute paths should be measured from the root of the filesystem (which developers generally do not have access to) or from some other location that makes more sense for your application. Now, there are some consistent guidelines for filesystem use on iOS:

  • Relative paths are now considered to be relative to the javascript file they are being executed from.
  • Absolute paths are now considered to be relative to the application resources directory (i.e. the same directory where your app.js is stored).
  • All other paths – including those returned by Titanium.Filesystem properties – are now file URIs. For example, Titanium.Filesystem.resourcesDirectory is now a file URI pointing to the resources directory of the application (where the app.js is stored) – including a trailing slash.

For example:

Will log what you’d hope it would (‘X is: Hello!’) to the console.

For those of you writing custom modules which take advantage of the filesystem, to conform to these new guidelines, you will need to make sure that you handle paths in a manner consistent with Titanium. We provide two internal methods in our source which you can use for this: +[TiUtils toURL:proxy:] and +[TiUtiuls toURL:relativeToURL:]. These translate string paths to NSURL objects, which can then have their filesystem path component extracted.

1 COMMENT

  1. Thanks for this.

    Looking forward to seeing the demo code in the KitchenSink filesystem.js file to reflect these changes. 🙂

    Especially when it comes to file writing, deleting, renaming and moving along with directory creation, listing and deleting – with respect to the Ti.Filesystem.applicationDataDirectory which we have write access to.

    Code strong etc! 😉

  2. Is there a way in JS using the Ti SDK to tap in to the Utils functions you mention to convert a property like applicationDataDirectory to the old format?

    To basically convert the file://localhost/blah/blah/blah%20directory/blah/

    Or to I need to use basic JS like :

    var appDataDir = Ti.Filesystem.applicationDataDirectory.slice(0,Ti.Filesystem.applicationDataDirectory.length – 1).replace(‘file://localhost’,”).replace(/%20/g,’ ‘);