iOS检测版本更新
效果图

HSUpdateApp.gif
实现思路
1.本地检测项目版本号;
2.联网检测项目在AppStore上的版本号;
3.比较版本号,跳转到手机自带的AppStore项目页面供用户下载;
具体方法
1.本地获取当前项目版本号:
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];//currentVersion为当前工程项目版本号
2.联网检测项目在AppStore上的版本号(利用天朝接口http://itunes.apple.com/cn/lookup?id=你的项目ID):
NSError *error;
NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",storeAppID]]] returningResponse:nil error:nil];
if (response == nil) {
NSLog(@"你可能没有连接网络哦");
return;
}
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"hsUpdateAppError:%@",error);
return;
}
NSLog(@"可输出一下看看%@",appInfoDic);
NSArray *array = appInfoDic[@"results"];
if (array.count < 1) {
NSLog(@"此APPID为未上架的APP或者查询不到");
return;
}
NSDictionary *dic = array[0];
//商店版本号
NSString *appStoreVersion = dic[@"version"];
3.比较版本号,跳转到手机自带的AppStore项目页面供用户下载:
NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
//设置版本号,主要是为了区分不同的版本,比如有1.2.1、1.2、1.31各种类型
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"];
}
//4当前版本号小于商店版本号,就更新
if([currentVersion floatValue] < [appStoreVersion floatValue])
{
UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//此处加入应用在app store的地址,方便用户去更新,一种实现方式如下
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]];
[[UIApplication sharedApplication] openURL:url];
}];
UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alercConteoller addAction:actionYes];
[alercConteoller addAction:actionNo];
[self presentViewController:alercConteoller animated:YES completion:nil];
}else{
NSLog(@"版本号好像比商店大噢!检测到不需要更新");
}
链接:http://www.jianshu.com/p/1d08c786b52f
來源:wolfhous简书
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?