iOS 判断首次启动

1.判断是否首次启动,用NSUserDefaults记下版本号来判断


{
    BOOL firstLaunching = false;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *lastAppVersion = [userDefaults objectForKey:@"LastAppVersion"];
    NSString *currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    if ([lastAppVersion floatValue] < [currentAppVersion floatValue])
    {
        [userDefaults setValue:currentAppVersion forKey:@"LastAppVersion"];
        [userDefaults synchronize];
        
        firstLaunching = true;
    }
    
    return firstLaunching;
}


2.判断是否今天首次启动

+ (BOOL)isFirstLaunchingToday{
    
    BOOL isFirstLaunch = YES;
    //判断是否是当天第一次打开程序
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyyMMdd"];
    
    //上次进入时间
    NSUserDefaults *userDefualt      = [NSUserDefaults standardUserDefaults];
    NSString *enterTimeString        = [userDefualt objectForKey:@"launchTimeKey"];
    NSTimeInterval enterTimeInterval = [enterTimeString doubleValue];
    NSDate *erverLaunchDate = [NSDate dateWithTimeIntervalSince1970:enterTimeInterval];
    
    //当前时间
    NSInteger erverLauningInteger = [[formater stringFromDate:erverLaunchDate] integerValue];
    NSDate *currentLaunchDate = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]];
    NSInteger currentlaunchingInteger = [[formater stringFromDate:currentLaunchDate] integerValue];
    
    //如果第二次打开与上一次打开不在同一天,那第二次打开就是当天的第一次打开
    if (currentlaunchingInteger - erverLauningInteger >= 1) {
        isFirstLaunch = YES;
    }
    else{
        //同一天
        isFirstLaunch = NO;
    }
    return isFirstLaunch;
    
}


你可能感兴趣的:(iOS 判断首次启动)