iOS APP检查版本更新

在开发中,我们可能会遇到这样的需求,当AppStore中有新版本需要更新迭代,用户在点开APP的时候弹出提示框提醒用户去AppStore更新。 这里需要我们判断当前APP与AppStore中的版本差别,如果一样,不需要提示;如果不一样就弹出提示框,给用户提示更新APP版本。

-(void)viewDidLoad{
    [super viewDidLoad];
    //iOS应用版本检测
    [self versionDetection];
}
-(void)versionDetection{
    //获取当前发布的版本的Version
    NSString *currentVersionStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=1061858098"] encoding:NSUTF8StringEncoding error:nil];
    //判断字符串是否为nil,长度是不是大于0,以及是不是取得版本
    if (currentVersionStr != nil && [currentVersionStr length] > 0 && [currentVersionStr rangeOfString:@"version"].length == 7) {
        [self checkAppUpdate:currentVersionStr];
    }
}
复制代码

代码中的url是每个APP的链接,其中id可以在下图中查到,这个id是上线后自动分配的唯一id,这个id还有其他的查看方式,这里就不说了。由于公司app的销售范围是国内,所以链接是itunes.apple.com/cn/lookup?i…,如果销售范围不只国内链接需改成itunes.apple.com/lookup?id=1…,否则拿到的results会为空

-(void)checkAppUpdate:(NSString *)currentVersionStr{
    //string转dictionary
    NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersionStr];
    //获取当前版本
    NSString *versionStr = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    //线上App版本
    NSString *appVersion = [currentVersionStr substringFromIndex:[currentVersionStr rangeOfString:@"\"version\":"].location+10];
    appVersion = [[appVersion substringToIndex:[appVersion rangeOfString:@","].location] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSArray *localArray = [versionStr componentsSeparatedByString:@"."];
    NSArray *versionArray = [appVersion componentsSeparatedByString:@"."];
    //这里比较版本号不是不一样就提示更新升级。而是当前版本号如果比AppStore版备号小的时候提示弹框升级。这样做的最大好处就是苹果在审核App时不会出现提示升级。
    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self uodateVersion:currentVersionStr];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
               [self uodateVersion:currentVersionStr];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self uodateVersion:currentVersionStr];
                }
            }
        }
    }
-(void)uodateVersion:(NSString *)currentVersion {
        NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersion];
        NSString * releaseNotesString= [[[versionDict objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发现新版本" message:releaseNotesString preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *url = @"https://itunes.apple.com/cn/app/de-wei-dian-shang/id1061858098?mt=8&uo=4";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
}
复制代码

Demo

纠正,现在这种方式会被苹果给拒绝,可以换成后台控制这个弹出框的显示 结语:如有错误请留言! 如果疑问请留言,一起进步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个用 Objective-C 实现的检测 App 版本的例子: ``` NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; NSString *appStoreURL = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", APP_ID]; // APP_ID是你的应用程序在App Store中的ID NSURL *url = [NSURL URLWithString:appStoreURL]; NSString *appData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:[appData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; NSString *latestVersion = [[[jsonData objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"]; if (![latestVersion isEqualToString:currentVersion]) { // 弹出提醒用户升级的对话框 } ``` 如果你想检测是否有新版本,并且提醒用户升级,可以在上面的代码的最后加上相应的逻辑。 注意事项:这段代码中的 APP_ID 和 iTunes URL 都需要替换成你自己的。此外,为了避免网络请求阻塞主线程,你应该在后台线程中执行网络请求操作。 ### 回答2: iOS系统通过检测app版本可以帮助用户及时获取应用程序的最新功能和修复的Bug,提高用户体验。一般而言,iOS检测app版本的例子可以通过以下几种方式实现: 1. 通过App Store的更新提示:用户可以通过打开App Store,点击右下角的“更新”选项,系统会自动检测已安装应用的版本并显示需要更新的应用程序。用户可以根据需要选择更新或手动更新应用。 2. 后台自动检测:在应用程序启动时,可以通过后台接口请求服务器,检测是否有新版本可供下载。服务器可以提供一个存储最新版本版本号,应用程序在启动时获取当前版本号与服务器的版本号进行比较,如不一致则提示用户有新版本可用。 3. 弹窗提醒:应用程序可以设置一个弹窗提醒用户有新版本可供下载。当用户打开应用程序时,如果检测到有新版本,则弹窗提醒用户前往App Store下载更新。 4. 版本检测接口:应用程序可以与服务器建立连接,通过接口请求服务器获取最新版本信息。服务器返回最新版本号及版本更新内容,应用程序解析后展示在界面上,让用户自行进行更新。 以上是iOS检测app版本的一些例子,具体的实现方式可以根据开发者的需求和业务场景进行选择和定制。 ### 回答3: iOS检测app版本的一个例子是通过比较当前安装的app版本与服务器上最新的app版本来判断是否需要更新。 这个过程可以分为以下几个步骤: 1. 首先,我们需要获取当前设备上安装的app版本号。可以通过在代码中获取Info.plist文件中的CFBundleShortVersionString键对应的值来实现,该值表示了app的当前版本号。 2. 下一步是从服务器上获取最新的app版本号。一种常见的做法是,后台维护一个存储了最新app版本号以及相关信息的接口。我们可以通过向这个接口发送请求,获取服务器上存储的最新app版本号。 3. 接着,我们将当前设备上的版本号与服务器上的最新版本号进行比较。可以将版本号转换为整数或浮点数,然后进行比较操作。如果设备上的版本号小于服务器上的最新版本号,则说明需要进行更新。 4. 最后,如果需要进行更新,我们可以向用户展示一个弹窗或者推送一个通知,告知他们有新版本可用,并提供下载连接或提示他们前往App Store进行更新。 此外,我们还可以在app启动时进行版本检测,以保障用户使用的是最新的版本。在每次启动或者在用户打开特定页面时,我们可以异步发送一个请求到服务器,检查最新版本号。如果有更新,我们可以选择直接跳转到更新页面或者提供提示给用户。 总之,通过比较当前设备上的版本号和服务器上的最新版本号,我们可以便捷地实现iOS检测app版本的功能,及时提醒用户更新并提供下载链接,以提供更好的用户体验和功能支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值