NSUserDefaults行为与应用程序更新 [英] NSUserDefaults behaviour with app update
问题描述
我的iOS应用程序中有一个方法,可以在检测到服务器的应用程序版本更高(新的ipa版本)时更新应用程序.如果用户要下载它,则该应用程序会在iPad上进行自我更新.
I have a method in my iOS app that updates the application when detects when my server has a greater version for my app (a new ipa version). If the user wants to download it, the app updates itself on the iPad.
问题是,当应用程序首次打开新版本时,我想更新数据库中的某些实体,但是我不确定该怎么做.下载最新的ipa时,由于XCode应用程序崩溃,我无法调试它.
The thing is that I want to update some entities atributes from the DB when the app opens the new version for the first time, but i'm not sure how to. I can't debug it cause when I download the latest ipa, for XCode the app crashed.
我正在考虑在AppDelegate.m中执行以下操作:
I was thinking about doing something like this in the AppDelegate.m:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
//do the stuff i wanna do
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
// This is the first launch ever
}
但是我不知道此[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]在更新之前是否设置为YES,因为该过程应该是:
But I don't know if this [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"] was set to YES before the update, cause the process should be:
1)首次启动该应用程序.2)该应用检测到较新版本.3)下载相同的应用程序->此时,苹果将旧版本替换"为较新版本.4)打开较新版本的应用程序.5)只在我第一次发布新版本时才做我想做的事情.
1)Launch the app for the first time ever. 2)The app detects a newer version. 3)Download the same app -> At this point apple "replace" the older version to the newer one. 4)Open the newer version app. 5)Do the stuff i wanna do ONLY for the first time I launch the new version.
推荐答案
您可以使用存储在NSUserDefaults中的整数,该整数的版本号针对该应用的每个版本进行了硬编码.如果该整数小于硬编码版本,则提示进行更新:
You could use an integer stored in NSUserDefaults with a version number hard-coded for each version of the app. If the integer is lower than the hard-coded version, prompt for updates:
NSInteger currentVersion = 3; // increment with each new version
if ([[NSUSerDefaults standardUserDefaults] integerForKey:@"HasLaunchedForVersion"] < currentVersion) {
[[NSUserDefaults standardUserDefaults] setInteger:currentVersion forKey:@"HasLaunchedForVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
// This is the first launch for this version
} else {
// App hasn't been updated since last launch
}
这篇关于NSUserDefaults行为与应用程序更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!