检测版本更新

//生成属性  当前版本  和  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];

}

温馨提示  可以将检测版本 和 更新版本 放到一个方法(分两个方法 是为了能够根据不同情况来判断控件的文字显示)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有些App更新新功能并且上线后,会有这样的需求:用户登录后,会提示用户“发现新版本,请更新”,这就需要用户点击确定...
    海耐射手阅读 5,311评论 4 2
  • 参考文章:http://blog.csdn.net/xiaoxuan415315/article/details/...
    Isspace阅读 1,115评论 0 1
  • 1.一定要先配置自己项目在商店的APPID(从苹果开发者中心登录自己的账号可看),配置完最好在真机上运行才能看到完...
    傲骨天成科技阅读 418评论 0 1
  • 基于良好的用户体验,当在appStroe上线了新的版本时,提醒用户更新”新版本“是必不可少的。 版本更新的...
    ninazhang阅读 1,027评论 5 1
  • 她留下了最狠的名,却有这世间最柔的情,她逆了众生,逆了苍天,却忘不掉那一个人,她有笑有泪,还有数不尽的相思,她似神...
    流梦逆羽阅读 310评论 0 0