我想检查用户是否使用了最新版本的应用。如果是这样,我应该为一些细胞的背景着色。
这是我正在使用的代码:
appDelegate.m
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
我想检查的方法:
NSString *lastVersion = (NSString *) [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
lastVersion = @"1.3.4";
if(![lastVersion isEqualToString:version]){
cell.backgroundColor = [UIColor yellowColor]; ;
cell.imageView.image = [UIImage imageNamed:@"image.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
我做得对吗?我的代码是否检查最新版本或?如何在模拟器\设备上模拟此行为?
我想要做的是检查用户是否使用最新版本的应用程序,以便调用另一个NSUserDefaults键以显示具有不同背景颜色的tableView的单元格。
修改
代码我用来更改单元格背景,如果用户使用的是我的应用程序的最新版本,并且用户使用该应用程序的次数少于三次:
NSString *cellValue = cell.textLabel.text;
NSNumber *runNumber = (NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:@"runNum"];
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
if (!runNumber) {
runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:@"runNum"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([lastVersion isEqualToString:version]){ //check if the user is using the latest version
if ([runNumber intValue] < 4) { //check if user has used the app less than 3 times
if ([cellValue isEqual: "someText"] ){
cell.backgroundColor = [UIColor yellowColor]; ;
cell.imageView.image = [UIImage imageNamed:@"image.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
}
答案 0 :(得分:4)
此代码不会设置任何内容..
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
lastVersion = @"1.3.4";
你需要一个二传手:
[[NSUserDefaults standardUserDefaults] setObject: @"1.3.4" forKey:@"lastVer"];//in memory
[[NSUserDefaults standardUserDefaults] synchronize];//to disk
和一个吸气者:
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
编辑:
请记住,lastVersion是一个指针,您只需将其设置为指向其他位置,不会将其设置为stdUserDefaults,也不会将其与磁盘同步。
答案 1 :(得分:1)
当然,如果您想检查用户是否使用最新版本的应用,您需要使用外部资源进行检查。
e.g。
对您自己的服务器的简单HTTP请求,返回您最新的应用版本。
然后将其与您当前的版本[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
将此测试的结果存储在NSUserDefaults