IOS中如何判断APP是否安装后首次运行或升级后首次运行

对于是否为首次安装的App可以使用如下方法来判断

[[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];

如果App是通过App Store升级的话可以通过版本号来判断

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

 

实际使用可以在didFinishLaunchingWithOptions:中添加如下代码

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"安装之后首次开启App");
        //获取当前App版本号并存入NSUserDefaults中
        NSString* appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:appVersion forKey:@"appVersion"];
        [userDefaults synchronize];
    }
    else {
        //根据版本号判断是否更新后首次启动
        NSString* appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString* localVersion = [userDefaults stringForKey:@"appVersion"];
        if ([appVersion isEqualToString:localVersion]) {
            NSLog(@"安装或升级之后并非首次开启App");
        }
        else {
            NSLog(@"升级之后首次开启App");
            //将当前App版本号存入NSUserDefaults中
            [userDefaults setObject:appVersion forKey:@"appVersion"];
            [userDefaults synchronize];
        }
    }

 

posted @ 2014-10-29 18:13  Dino.Tang  阅读(2373)  评论(0编辑  收藏  举报