• iOS


    iOS 想要检查 App 当前版本是否为最新,一般的方案大概都是服务器自己提供一个接口来获取 App 最新版本是多少,然后再做出相应提示是否需要更新,但是接口需要手动维护,应用要审核,还得等审核通过以后才能更新版本号,其实苹果提供了一个 iTunes 接口,能够查到 App 在 AppStore 上的状态信息,既省事又准确,下面记录一下具体实现方法。

    接口信息

    • 这是 iTunes 接口地址 ,有兴趣可以看一下,我们要用到的接口如下,xxx 处换成自己 App 的 AppId ,AppId 可以在 iTunes Connect 里面看到。

    http://itunes.apple.com/lookup?id=xxx
    • 接口返回的内容有很多,下面展示一些有用的看看。

    {
        "resultCount" : 1,
        "results" : [{
            "artistId" : "开发者 ID",
            "artistName" : "开发者名称",
            "trackCensoredName" : "审查名称",
            "trackContentRating" : "评级",
            "trackId" : "应用程序 ID",
            "trackName" = "应用程序名称",
            "trackViewUrl" = "应用程序下载网址",
            "userRatingCount" = "用户评论数量",
            "userRatingCountForCurrentVersion" = "当前版本的用户评论数量",
            "version" = "版本号"
        }]
    }

    实现方法

    下面是检查版本更新的具体实现方法,注意接口地址 xxx 处换成自己 App 的 AppId ,App 审核的时候版本肯定是比 AppStore 上高的,所以不用担心审核时会跳出更新提示。

    /// 检查版本更新
    - (void)checkVersion {
        NSString *url = @"http://itunes.apple.com/lookup?id=xxx";
        [[AFHTTPSessionManager manager] POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            DLog(@"版本更新检查成功");
            NSArray *results = responseObject[@"results"];
            if (results && results.count > 0) {
                NSDictionary *response = results.firstObject;
                NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; // 软件的当前版本
                NSString *lastestVersion = response[@"version"]; // AppStore 上软件的最新版本
                if (currentVersion && lastestVersion && ![self isLastestVersion:currentVersion compare:lastestVersion]) {
                    // 给出提示是否前往 AppStore 更新
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"检测到有版本更新,是否前往 AppStore 更新版本。" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                        NSString *trackViewUrl = response[@"trackViewUrl"]; // AppStore 上软件的地址
                        if (trackViewUrl) {
                            NSURL *appStoreURL = [NSURL URLWithString:trackViewUrl];
                            if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
                                [[UIApplication sharedApplication] openURL:appStoreURL];
                            }
                        }
                    }]];
                    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
                }
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            DLog(@"版本更新检查失败");
        }];
    }
    /// 判断是否最新版本号(大于或等于为最新)
    - (BOOL)isLastestVersion:(NSString *)currentVersion compare:(NSString *)lastestVersion {
        if (currentVersion && lastestVersion) {
            // 拆分成数组
            NSMutableArray *currentItems = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];
            NSMutableArray *lastestItems = [[lastestVersion componentsSeparatedByString:@"."] mutableCopy];
            // 如果数量不一样补0
            NSInteger currentCount = currentItems.count;
            NSInteger lastestCount = lastestItems.count;
            if (currentCount != lastestCount) {
                NSInteger count = labs(currentCount - lastestCount); // 取绝对值
                for (int i = 0; i < count; ++i) {
                    if (currentCount > lastestCount) {
                        [lastestItems addObject:@"0"];
                    } else {
                        [currentItems addObject:@"0"];
                    }
                }
            }
            // 依次比较
            BOOL isLastest = YES;
            for (int i = 0; i < currentItems.count; ++i) {
                NSString *currentItem = currentItems[i];
                NSString *lastestItem = lastestItems[i];
                if (currentItem.integerValue != lastestItem.integerValue) {
                    isLastest = currentItem.integerValue > lastestItem.integerValue;
                    break;
                }
            }
            return isLastest;
        }
        return NO;
    }
  • 相关阅读:
    Entity Framework Code-First(10.3):Property Mappings
    Entity Framework Code-First(10.2):Entity Mappings
    Entity Framework Code-First(10.1):EntityTypeConfiguration
    Entity Framework Code-First(10):Fluent API
    Entity Framework Code-First(9.11):DataAnnotations
    Entity Framework Code-First(9.10):DataAnnotations
    Entity Framework Code-First(9.9):DataAnnotations
    Entity Framework Code-First(9.8):DataAnnotations
    Entity Framework Code-First(9.7):DataAnnotations
    JAVA-初步认识-第六章-类与对象体现
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/9056104.html
Copyright © 2020-2023  润新知