Monday 21 November 2016

Quick Tip - How to get the path of a file in the main bundle?

If you include files in your Firemonkey project with a remote path of ./ they're copied into the main bundle when the app is built and deployed on iOS.

To access these files, you need to get their full path as follows:

Result:=NSStrToStr(TiOSHelper.MainBundle.pathForResource(StrToNSStr(fn), StrToNSStr(ext)));

fn = the name of the file without the extension
ext = the extension without the starting period (e.g. "caf' not ".caf")

If the file exists in the bundle it's full path and filename will be returned, otherwise it'll be an empty string.

You'll need to include these units in your uses:

uses
iOSapi.Helpers, Macapi.Helpers;