仅在应用更新后调用的方法

时间:2013-11-27 22:25:31

标签: ios objective-c

我有一个场景,每当用户更新应用程序或进行全新安装时,我只需要调用一次方法。 所以我的问题是在App Delegate中有任何特定的方法被调用,在我们更新应用程序之后,所以如果有人可以告诉我另一种方法来实现这一点,那将非常感激。

2 个答案:

答案 0 :(得分:5)

在您调用函数一次后,我会在NSUserDefaults中设置一个值。请在此处查看已接受的答案:iOS : Call a method just one time

如果您需要在更新后运行,则应包括一个始终增加的数字,或拉取内部版本号,并检查保存的值。这样您就可以了解该应用是否已更新。

答案 1 :(得分:0)

我正在编写以下功能,该功能唯一地检查用户是否已评级当前版本,[self nowRate]是该功能,请将此更改为您希望每个版本执行的任何操作

-(void)rateApp {
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSMutableDictionary*appRatedForVersion = [[NSMutableDictionary alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[[NSUserDefaults standardUserDefaults] dictionaryRepresentation].allKeys containsObject:@"AppRateDictionaryData"]){
    NSData *versionRateData = [defaults objectForKey:@"AppRateDictionaryData"];
    appRatedForVersion = [NSKeyedUnarchiver unarchiveObjectWithData:versionRateData];
    if ([appRatedForVersion objectForKey:version] != nil) {
        NSNumber *isAppRatedForCurrentVersion = [appRatedForVersion objectForKey:version];
        BOOL hasBeenRated = [isAppRatedForCurrentVersion boolValue];
        if (hasBeenRated) {
            //Action already performed in this version do whatever you like
        } else {
            [self nowRate];
        }
    } else {
        [appRatedForVersion setObject:[NSNumber numberWithBool:YES] forKey:version];
        NSData *versionRateData1 = [NSKeyedArchiver archivedDataWithRootObject:appRatedForVersion];
        [defaults setObject:versionRateData1 forKey:@"AppRateDictionaryData"];
        [defaults synchronize];
        [self nowRate];
    }
} else {
    [appRatedForVersion setObject:[NSNumber numberWithBool:YES] forKey:version];
    NSData *versionRateData = [NSKeyedArchiver archivedDataWithRootObject:appRatedForVersion];
    [defaults setObject:versionRateData forKey:@"AppRateDictionaryData"];
    [defaults synchronize];
    [self nowRate];
}

}