How to detect an iOS App has been re-installed (from XCode) or upgraded (from AppStore)

会有一股神秘感。 提交于 2019-12-01 09:01:10

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!