NSString *appleID = @"your app appleID";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSMutableDictionary *jsondata = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"jsondata===%@",jsondata);
NSMutableArray *resultsArr = [jsondata objectForKey:@"results"];
NSMutableDictionary *infodic = [resultsArr objectAtIndex:0];
NSString *latestVersion = [infodic objectForKey:@"version"];
NSString *trackViewUrl = [infodic objectForKey:@"trackViewUrl"];
NSLog(@"%@",trackViewUrl);
NSLog(@"latestVersion=%@",latestVersion);
[[NSUserDefaults standardUserDefaults]setObject:trackViewUrl forKey:@"trackViewUrl"];
NSLog(@"uurrll=%@",trackViewUrl);
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
NSLog(@"当前版本号是%@",currentVersion);
NSLog(@"doublecurrent=%@",currentVersion);
if ([currentVersion isEqualToString:latestVersion]) {
NSLog(@"版本相同 不用更新");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"updateVersion"];
[[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"updateVersion"];
}else
{ NSArray * newVerAry = [latestVersion componentsSeparatedByString:@"."];
NSArray * curVerAry = [currentVersion componentsSeparatedByString:@"."];
for (int i = 0; i < [newVerAry count] || i < [curVerAry count]; i++)
{
int newSubVer, curSubVer;
// 如果子版本号数不足,作为0处理
if (i >= [newVerAry count])
{
newSubVer = 0;
}
else
{
newSubVer = [[newVerAry objectAtIndex:i] intValue];
}
if( i >= [curVerAry count])
{
curSubVer = 0;
}
else
{
curSubVer = [[curVerAry objectAtIndex:i] intValue];
}
if( newSubVer > curSubVer)
{
NSLog(@"当前版本低 ,需要更新");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"updateVersion"];
[[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"updateVersion"];
NSLog(@"version == yes");
return;
}
else if( newSubVer < curSubVer)
{
NSLog(@"当前版本高 不需要更新");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"updateVersion"];
[[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"updateVersion"];
return;
}
}
}
上边是完整的判断代码,判断结果保存在UserDefaults里边,“yes”为有新版本,“no”为没有新版本。
该方法适用于APP版本为1.0.0这种格式。