两步完成iOS App版本更新提示

最近app里集合了更新

- (void)updateApp
{
   NSError *error;
  //kAPP_URL : http://itunes.apple.com/lookup?id=
  //kAppId : 在iTunes connect上申请的APP ID

NSString *appID;
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];

appID = @"108873****";

NSString *appUrl = @"http://itunes.apple.com/lookup?id=";
NSString *urlStr = [NSString stringWithFormat:@"%@%@", appUrl, appID];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

if (error) {
    NSLog(@"%@", error.description);
    return;
}

NSArray *resultArray = [appInfoDict objectForKey:@"results"];

if (![resultArray count]) {
    NSLog(@"error : resultArray == nil");
    return;
}

NSDictionary *infoDict = [resultArray objectAtIndex:0];
//获取服务器上应用的最新版本号
NSString *updateVersion = infoDict[@"version"];
NSString *trackName = infoDict[@"trackName"];
NSString *note = [infoDict objectForKey:@"releaseNotes"];

_trackViewUrl = infoDict[@"trackViewUrl"];

//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"] ;

//判断两个版本是否相同
if ([updateVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {
    NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
    
    alert.tag = 999;
    [alert show];
    
  }

}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:      (NSInteger)buttonIndex
{
if (alertView.tag == 999) {
    if (buttonIndex == 1) { //点击”升级“按钮,就从打开app store上应用的详情页面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
    }
}
}

你可能感兴趣的:(两步完成iOS App版本更新提示)