iOS 判断版本是否升级,若是有新的版本,提醒升级

/**
 *  ldz
 *
 *  检测软件是否需要升级
 */
-(void)checkVersion
{
    //获取当前应用版本号
    NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
    //  用__block才能在局部作用域中改变变量的值
    __block NSString *newVersion = @"";
    NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%ld",(long)[iFeverAPPID integerValue]];
    
    NSString * str = [updateUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"123 + %@", operation);
        NSDictionary *resultDic = responseObject;
        NSArray *resultArray = [resultDic objectForKey:@"results"];
        
        for (id config in resultArray) {
            newVersion = [config valueForKey:@"version"];
        }
        if (newVersion) {
            NSLog(@"通过AppStore获取的版本号是:%@", newVersion);
        }
        
        if ([newVersion floatValue] > [currentVersion floatValue]) {
            NSString *versionMessageStr = [NSString stringWithFormat:@"当前版本%@,最新版本为%@,请升级.",currentVersion,newVersion];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"升级提示!" message: versionMessageStr delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles:@"现在升级", nil];
            alert.tag = kVersionNeedUpdateAlertTag;
            [alert show];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"234 + %@", error);
    }];
}


#pragma mark - UIAlertDelegate Method
//收到推送时程序正在前台运行,则给出一个alert,用户选择查看,执行这个方法,并且跳转到指定页面
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        //软件需要更新提醒
        if (alertView.tag == kVersionNeedUpdateAlertTag) {
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/wan-zhuan-quan-cheng/id%@?mt=8",iFeverAPPID]];
            [[UIApplication sharedApplication]openURL:url];
            /*
             // 打开iTunes 方法二:此方法总是提示“无法连接到itunes”,不推荐使用
             NSString *iTunesLink = @"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=%i&mt=8";
             NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=%i&mt=8",iFeverAPPID]];
             [[UIApplication sharedApplication] openURL:url];
             */
        }
    }
}



//注意

iFeverAPPID 是你公司已经上线的APP ID

kVersionNeedUpdateAlertTag 是为了分别你AppDelegate页面中有多个alertView



你可能感兴趣的:(iOS 判断版本是否升级,若是有新的版本,提醒升级)