首页 > 代码库 > iOS 版本更新检查

iOS 版本更新检查

//检查更新

-(void)onCheckVersion

{

//获取当前版本

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];//CFBundleVersion

//POST 请求获取APPStore上的版本信息    

    NSString *URL = @"http://itunes.apple.com/lookup?id=APP_ID";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];

    NSHTTPURLResponse *urlResponse = nil;

    NSError *error = nil;

    NSData *recervedData = http://www.mamicode.com/[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];

    NSArray *infoArray = [dic objectForKey:@"results"];

    

    if ([infoArray count]) {

        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];

        NSString *lastVersion = [releaseInfo objectForKey:@"version"];

        

        if (![lastVersion isEqualToString:currentVersion]) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:[releaseInfo  objectForKey:@"releaseNotes"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往更新", nil];

            alert.tag = 10000;

            [alert show];

        }

        else

        {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            alert.tag = 10001;

            [alert show];

        }

    }

}

 

#pragma mark - AertView delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (alertView.tag == 10000) {

        

        if (buttonIndex == 1) {

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_URL]];

        }

    }

}

iOS 版本更新检查