iOS 检查版本号的代码

- (void)checkNewVersion{

    if ([@"appStore" isEqualToString:CHANNEL]) {

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", APPLE_ID];

        [manager POST:url sign:nil token:self.user.token parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

            id obj = [responseObject objectForKey:@"results"];

            if (obj && [obj isKindOfClass:[NSArray class]]) {

                versionResult = [obj lastObject];    //NSMutableDictionary     *versionResult;

                if ([versionResult isKindOfClass:[NSMutableDictionary class]]) {//versionResult是全局的

                    NSString *newVersion = [versionResult objectForKey:@"version"];//appstore的版本

                    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

                    NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];//本地软件版本

                    if (newVersion && [currentVersion compare:newVersion] == NSOrderedAscending) {

                        UIAlertView *newVersionAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"检测到可用的新版本%@",newVersion]

                                                                                  message:[NSString stringWithFormat:@"%@",[versionResult objectForKey:@"releaseNotes"]]

                                                                                 delegate:self

                                                                        cancelButtonTitle:@"稍后再说"

                                                                        otherButtonTitles:@"马上升级",nil];

                        newVersionAlert.tag = NEW_VERSION_ALERT;//用于标记当前alert

                        [newVersionAlert show];

                    }

                }

            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"Error: %@", error);

        }];

    } else {

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        NSString *url = CHECK_VERSION_URL;

        [manager POST:url sign:nil token:self.user.token parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSString *newVersion = [responseObject objectForKey:@"vcode"];

            NSString *newDesc = [responseObject objectForKey:@"vdesc"];

            NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

            NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];//本地软件版本

            if (newVersion && [currentVersion compare:newVersion] == NSOrderedAscending) {

                UIAlertView *newVersionAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"检测到可用的新版本%@",newVersion]

                                                                          message:newDesc

                                                                         delegate:self

                                                                cancelButtonTitle:@"稍后再说"

                                                                otherButtonTitles:@"马上升级",nil];

                newVersionAlert.tag = NEW_VERSION_ALERT;//用于标记当前alert

                [newVersionAlert show];

            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"Error: %@", error);

        }];

    }

}

//点击马上升级的代理

#pragma mark UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    [super alertView:alertView clickedButtonAtIndex:buttonIndex];

    if (alertView.tag == NEW_VERSION_ALERT) {

        //新版本提示框

        if (buttonIndex == 1) {

            if ([@"appStore" isEqualToString:CHANNEL]) {

                //跳转到App Store

                if ([versionResult isKindOfClass:[NSMutableDictionary class]]) {

                    UIApplication *application = [UIApplication sharedApplication];

                    NSString *trackViewUrl = [versionResult objectForKey:@"trackViewUrl"];

                    [application openURL:[NSURL URLWithString:[trackViewUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

                }

            } else {

                UIApplication *application = [UIApplication sharedApplication];

                NSString *trackViewUrl = APP_DOWNLOAD_URL;

                [application openURL:[NSURL URLWithString:[trackViewUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

            }

        }

    }

}

 

 

//黄色标记的为,#define得到的

如果需要检查版本的话,可直接复制一下代码,其中再大致修改一下

转载于:https://www.cnblogs.com/lyz0925/p/4995049.html

  • 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、付费专栏及课程。

余额充值