//生成属性 当前版本 和 appStore上的版本
@property (nonatomic, strong) NSString *currentVersion;
@property (nonatomic, strong) NSString *appStoreVersion;
#pragma mark -- 检测版本
- (void)checkAPPVersion {
// 先获取当前工程项目版本号
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
// NSLog(@"%@",infoDic);
NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];
self.currentVersion = currentVersion;
// 从网络获取AppStore版本号
NSError *error;
// STOREAPPID是你在AppStore对应自己App的ID 可在AppStore上找到 也可在iTunesConnect上找到
NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",appstore上的appID]]] returningResponse:nil error:nil];
if (response == nil) {
NSLog(@"没有连接网络");
return;
}
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"UpdateAppError:%@",error);
return;
}
NSArray *array = appInfoDic[@"results"];
if (array.count < 1) {
NSLog(@"此APPID为未上架的APP或者查询不到");
return;
}
dic = array[0];
_appStoreVersion = dic[@"version"];
//打印版本号
NSLog(@"+++当前版本号:%@\n商店版本号:%@",_currentVersion,_appStoreVersion);
// 设置版本号
_currentVersion = [_currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
if (_currentVersion.length==2) {
_currentVersion = [_currentVersion stringByAppendingString:@"0"];
}else if (_currentVersion.length==1){
_currentVersion = [_currentVersion stringByAppendingString:@"00"];
}
_appStoreVersion = [_appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
if (_appStoreVersion.length==2) {
_appStoreVersion = [_appStoreVersion stringByAppendingString:@"0"];
}else if (_appStoreVersion.length==1){
_appStoreVersion = [_appStoreVersion stringByAppendingString:@"00"];
}
}
#pragma mark -- 更换新版本
- (void)rechangeVersion {
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id苹果商店上的APPID?mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
温馨提示 可以将检测版本 和 更新版本 放到一个方法(分两个方法 是为了能够根据不同情况来判断控件的文字显示)