1.获取appStore的APP数据//获取appStore版本号
#pragma mark -- 获取appStore的APP数据 // 获取appStore版本号- (void)PostpathAPPStoreVersion{// 这是获取appStore上的app的版本的urlNSString*appStoreUrl = [[NSStringalloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"这里填app在appStore的id号"];NSURL*url = [NSURLURLWithString:appStoreUrl];NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10]; [request setHTTPMethod:@"POST"];NSOperationQueue*queue = [NSOperationQueuenew]; [NSURLConnectionsendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse*response,NSData*data,NSError*error){NSMutableDictionary*receiveStatusDic=[[NSMutableDictionaryalloc]init];if(data) {NSDictionary*receiveDic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableLeaveserror:nil];if([[receiveDic valueForKey:@"resultCount"] intValue]>0) { [receiveStatusDic setValue:@"1"forKey:@"status"]; [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"]; }else{ [receiveStatusDic setValue:@"-1"forKey:@"status"]; } }else{ [receiveStatusDic setValue:@"-1"forKey:@"status"]; } [selfperformSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO]; }];}
下面的 sender[@"version"] 就是获取的版本号 注意是String类型的
2 . 比较appStore的app版本号和本地的app的版本号
-(void)receiveData:(id)sender{NSDictionary*infoDictionary = [[NSBundlemainBundle] infoDictionary];// 手机当前APP软件版本 比如:1.0.2NSString*nativeVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];NSString*storeVersion = sender[@"version"]; DEBUGLog(@"本地版本号curV=%@", nativeVersion); DEBUGLog(@"商店版本号appV=%@", sender[@"version"]);NSComparisonResultcomparisonResult = [nativeVersion compare:storeVersion options:NSNumericSearch];//NSOrderedSame相同 NSOrderedAscending = -1L表示升序; NSOrderedDescending = +1 表示降序UIAlertView*alertv = [[UIAlertViewalloc]initWithTitle:@"有新版本是否更新"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];switch(comparisonResult) {caseNSOrderedSame: DEBUGLog(@"本地版本与商店版本号相同,不需要更新");break;caseNSOrderedAscending: DEBUGLog(@"本地版本号 < 商店版本号相同,需要更新"); [alertv show];break;caseNSOrderedDescending: DEBUGLog(@"本地版本号 > 商店版本号相同,不需要更新");break;default:break; }}
顺便把检测可能用到的其他信息也贴一下吧
pragma mark --检测版本等信息
- (void)PhoneAppVersionInfomations{NSString* identifierNumber = [[UIDevicecurrentDevice].identifierForVendor UUIDString] ; DEBUGLog(@"手机序列号: %@",identifierNumber);//手机别名: 用户定义的名称NSString* userPhoneName = [[UIDevicecurrentDevice] name]; DEBUGLog(@"手机别名: %@", userPhoneName);//设备名称NSString* deviceName = [[UIDevicecurrentDevice] systemName]; DEBUGLog(@"设备名称: %@",deviceName );//手机系统版本NSString* phoneVersion = [[UIDevicecurrentDevice] systemVersion]; DEBUGLog(@"手机系统版本: %@", phoneVersion);//手机型号NSString* phoneModel = [[UIDevicecurrentDevice] model]; DEBUGLog(@"手机型号: %@",phoneModel );//地方型号 (国际化区域名称)NSString* localPhoneModel = [[UIDevicecurrentDevice] localizedModel]; DEBUGLog(@"国际化区域名称: %@",localPhoneModel );NSDictionary*infoDictionary = [[NSBundlemainBundle] infoDictionary];// 当前应用名称NSString*appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"]; DEBUGLog(@"当前应用名称:%@",appCurName);// 当前应用软件版本 比如:1.0.1NSString*appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; DEBUGLog(@"当前应用软件版本:%@",appCurVersion);// 当前应用版本号码 int类型NSString*appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"]; DEBUGLog(@"当前应用版本号码:%@",appCurVersionNum);}