服务器上有一个 JSON 文件,其中包含四个关于应用程序中数据版本的“键/值”对(它是一本食谱,所以版本是食谱的版本) 当应用程序启动时,它会下载 JSON 并检查版本。
这是我的方法,但我认为它很慢。
- (void)isUpdatesAvail
{
updatesAvail = NO;
NSInteger iVer = [[NSUserDefaults standardUserDefaults] integerForKey:@"ingredientsVer"];
NSInteger rVer = [[NSUserDefaults standardUserDefaults] integerForKey:@"recipesVer"];
NSInteger iCnt = [[NSUserDefaults standardUserDefaults] integerForKey:@"ingredientsCount"];
NSInteger rCnt = [[NSUserDefaults standardUserDefaults] integerForKey:@"recipesCount"];
NSString *countPath = [downloadPath stringByAppendingString:@"/versioninfo"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:countPath]];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *error = nil;
if (!responseData) {
return;
}
NSDictionary *recipesCount = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (!recipesCount) {
return;
}
rCount = [recipesCount objectForKey:@"recipeCount"];
iCount = [recipesCount objectForKey:@"ingredientCount"];
rVersion = [recipesCount objectForKey:@"recipeVersion"];
iVersion = [recipesCount objectForKey:@"ingredientVersion"];
if ([iVersion integerValue] > iVer || [rVersion integerValue] > rVer || [iCount integerValue] > iCnt || [rCount integerValue] > rCnt) {
updatesAvail = YES;
}
}
有人可以给我建议(或可能是例子)如何做到这一点?