简体   繁体   English

NSUserDefaults行为与应用程序更新

[英]NSUserDefaults behaviour with app update

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). 我的iOS应用程序中有一种方法,可以在检测到服务器的应用程序版本更高时(新的ipa版本)来更新应用程序。 If the user wants to download it, the app updates itself on the iPad. 如果用户要下载它,则该应用程序会在iPad上进行自我更新。

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. 下载最新的ipa时,我无法调试它的原因,因为XCode应用程序崩溃了。

I was thinking about doing something like this in the AppDelegate.m: 我正在考虑在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
}

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: 但是我不知道此[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@“ HasLaunchedOnce”]在更新之前是否设置为YES,因为该过程应该是:

1)Launch the app for the first time ever. 1)首次启动该应用程序。 2)The app detects a newer version. 2)该应用检测到较新版本。 3)Download the same app -> At this point apple "replace" the older version to the newer one. 3)下载相同的应用程序->此时,苹果将旧版本“替换”为较新版本。 4)Open the newer version app. 4)打开较新版本的应用程序。 5)Do the stuff i wanna do ONLY for the first time I launch the new version. 5)只在我第一次发布新版本时才做我想做的事情。

You could use an integer stored in NSUserDefaults with a version number hard-coded for each version of the app. 您可以使用存储在NSUserDefaults中的整数,该整数具有针对该应用的每个版本的硬编码版本号。 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
}

They will not be removed. 它们不会被删除。 And will have old values. 并将具有旧的价值观。

if "HasLaunchedOnce" is used in the old version. 如果在旧版本中使用了“ HasLaunchedOnce”。 use a new one and call it "HasUpdateLaunchedOnce". 使用一个新的,并将其称为“ HasUpdateLaunchedOnce”。

Check both values and decide what do you want to do. 检查两个值,然后决定要做什么。

if (HasLaunchedOnce exists && HasUpdateLaunchedOnce not exists)
  // proceed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM