需求:当iOS应用迭代更新时,少不了更新提示
思路:通过获取appStore已上传的版本的版本号与手机当前该软件的plist文件中版本号对比来判断是否更新版本
直接上代码:
/**提示更新*/
- (void)upData {
//获取手机程序的版本号
NSString *ver = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
//POST必须上传的字段
NSDictionary *dict = @{@"id":@"Apple ID"};//此处的Apple ID
[mgr POST:@"https://itunes.apple.com/lookup" parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSArray *array = responseObject[@"results"];
if (array.count != 0) {// 先判断返回的数据是否为空
NSDictionary *dict = array[0];
//判断版本 [dict[@"version"] floatValue] > [ver floatValue]
if (dict[@"version"] > ver) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"更稳定、快速、多彩的功能和体验,立即点击升级!" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil] ;
alert.delegate = self;
alert.tag = 222;
[alert show];
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
上面在dict中的值为Apple ID,即是AppStore上的Apple ID
Paste_Image.png最后是AlertView的回调方法,跳到AppStore的下载地址
#pragma mark - AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 222) {
if (buttonIndex == 1) {
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString:@"AppStore的下载地址"]];
}
}
}
这样就实现了App应用的版本更新
�态度决定一切
网友评论