An app we are developing uses a binary file included in the app bundle.
As this file needs to be processed and in some cases re-written, the app copies this file into the Library/Application Support directory at the first launch or whenever it is missing in Application Support. Then the app loads this file from Application Support every time it is launched and uses the data contained in it.
As the Application Support directory is preserved at every app install/upgrade, we need to delete the above mentioned binary file under this directory when the app is installed or upgraded, re-copying it from the app bundle in order to be sure that the binary file structure is compatible with the latest version of the app.
We would need to do that in the following two cases:
- During the development, every time we rebuild and install the app from XCode
- Once live, every time the user upgrades the app from the AppStore
Is there any way to detect that the app has been installed or re-installed on the device?
The (probably) clean way of ensuring an update is using a variable in your info.plist file, as rmaddy said, but that wouldn't solve the problem of knowing precisely if it is needed or overkill.
I have honestly no idea if Apple will let it pass through their validation, but you can probably store and check the last modification date of the executable. If it's older than a certain date, then update. It seems a bit complicated to have such a crude versioning mechanism. Can't you include a version number in your binary file?
To check for the date:
NSString *exePath = [[NSBundle mainBundle] executablePath];
NSDictionary *exeAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:exePath error:nil];
NSString *lastModDate = [exeAttrs objectForKey:@"NSFileModificationDate"];
One general approach would be to have a "build number" in your Info.plist. You would need to update this number each time you build (or just each time the binary file changes and needs to be processed again).
Then when the app is started, it reads this number from the Info.plist. The app also gets the last known value it stores in NSUserDefaults
. If the number from the Info.plist is newer (or there is no value yet in NSUserDefaults
) then you know you must process the file. Then store the latest number in NSUserDefaults
. This will ensure that the next time the app is run, you won't process the file again (unless it's a new build or update).
The easiest way to do the app store case would be to store the app version in NSUserDefaults
and when the app version does not equal the value saved in NSUserDefaults
, you know that you need to recopy the file.
For the case when installing from Xcode, you could use something like the following
#ifdef DEBUG
// Force the file to be recopied
#else
// Check if the app version changed before recopying the file
#endif