iOS 利用iTunes接口检查App版本更新

作者: JerryLMJ | 来源:发表于2018-05-29 15:48 被阅读131次

    通常我们在做App版本检查更新功能的时候,都是后台服务器为我们提供一个获取App最新版本信息的接口,并且在每次新版本更新后,都需要人工在后台更改版本信息,费事且不及时。
    其实苹果提供了一个很友好的iTunes接口,供我们获取App Store中App当前的最新信息。

    接口:

    http://itunes.apple.com/lookup?id=xxx
    

    xxx换成相应App的AppId,例如微信的AppId为414478124,那么完整的url就是:http://itunes.apple.com/lookup?id=414478124
    拿微信举例,通过接口获取到的数据如下:

    {
        "resultCount": 1,
        "results": [{
                "artistViewUrl": "https://itunes.apple.com/us/developer/wechat/id614694882?uo=4",
                "artworkUrl60": "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/f1/2a/44/f12a44d4-86be-1902-8a3b-cc7d18f5cd6a/source/60x60bb.jpg",
                "artworkUrl100": "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/f1/2a/44/f12a44d4-86be-1902-8a3b-cc7d18f5cd6a/source/100x100bb.jpg",
                "screenshotUrls": [ "https://is1-ssl.mzstatic.com/image/thumb/Purple128/v4/bd/07/e9/bd07e908-249a-1cb2-bd3e-bc2f8c96a554/source/392x696bb.jpg",
                                    "https://is5-ssl.mzstatic.com/image/thumb/Purple118/v4/e7/14/fc/e714fc29-db02-b3d3-dfb0-bb3543f33ca4/source/392x696bb.jpg",
                                    "https://is5-ssl.mzstatic.com/image/thumb/Purple118/v4/fc/92/5a/fc925aea-d755-8c15-bae0-0f07cc0c13ea/source/392x696bb.jpg",
                                    "https://is5-ssl.mzstatic.com/image/thumb/Purple118/v4/ca/b5/3e/cab53e72-a6b7-e39c-4e34-6d76384d073d/source/392x696bb.jpg",
                                    "https://is2-ssl.mzstatic.com/image/thumb/Purple118/v4/43/f3/9d/43f39d80-a30b-32a6-37bc-d2aee81142d1/source/392x696bb.jpg" ],
                "ipadScreenshotUrls": [ "https://is4-ssl.mzstatic.com/image/thumb/Purple128/v4/a1/7e/20/a17e20b4-5725-d926-33d2-9e5f46128690/source/576x768bb.jpg",
                                        "https://is2-ssl.mzstatic.com/image/thumb/Purple118/v4/b5/f0/4f/b5f04f8f-f5c8-0f05-5406-72c0dfd2a99c/source/576x768bb.jpg",
                                        "https://is5-ssl.mzstatic.com/image/thumb/Purple118/v4/71/da/c6/71dac6a1-a506-b3f4-abe2-849831eccf92/source/576x768bb.jpg",
                                        "https://is5-ssl.mzstatic.com/image/thumb/Purple128/v4/bc/0d/7e/bc0d7ea5-bb31-47ed-9284-48c61775a50a/source/576x768bb.jpg" ],
                "appletvScreenshotUrls": [],
                "artworkUrl512": "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/f1/2a/44/f12a44d4-86be-1902-8a3b-cc7d18f5cd6a/source/512x512bb.jpg",
                "isGameCenterEnabled": false,
                "kind": "software",
                "features": [ "iosUniversal" ],
                "supportedDevices": [ "iPad2Wifi-iPad2Wifi",
                                      "iPad23G-iPad23G",
                                      "iPhone4S-iPhone4S",
                                      "iPadThirdGen-iPadThirdGen",
                                      "iPadThirdGen4G-iPadThirdGen4G",
                                      "iPhone5-iPhone5",
                                      "iPodTouchFifthGen-iPodTouchFifthGen",
                                      "iPadFourthGen-iPadFourthGen",
                                      "iPadFourthGen4G-iPadFourthGen4G",
                                      "iPadMini-iPadMini",
                                      "iPadMini4G-iPadMini4G",
                                      "iPhone5c-iPhone5c",
                                      "iPhone5s-iPhone5s",
                                      "iPadAir-iPadAir",
                                      "iPadAirCellular-iPadAirCellular",
                                      "iPadMiniRetina-iPadMiniRetina",
                                      "iPadMiniRetinaCellular-iPadMiniRetinaCellular",
                                      "iPhone6-iPhone6",
                                      "iPhone6Plus-iPhone6Plus",
                                      "iPadAir2-iPadAir2",
                                      "iPadAir2Cellular-iPadAir2Cellular",
                                      "iPadMini3-iPadMini3",
                                      "iPadMini3Cellular-iPadMini3Cellular",
                                      "iPodTouchSixthGen-iPodTouchSixthGen",
                                      "iPhone6s-iPhone6s",
                                      "iPhone6sPlus-iPhone6sPlus",
                                      "iPadMini4-iPadMini4",
                                      "iPadMini4Cellular-iPadMini4Cellular",
                                      "iPadPro-iPadPro",
                                      "iPadProCellular-iPadProCellular",
                                      "iPadPro97-iPadPro97",
                                      "iPadPro97Cellular-iPadPro97Cellular",
                                      "iPhoneSE-iPhoneSE",
                                      "iPhone7-iPhone7",
                                      "iPhone7Plus-iPhone7Plus",
                                      "iPad611-iPad611",
                                      "iPad612-iPad612",
                                      "iPad71-iPad71",
                                      "iPad72-iPad72",
                                      "iPad73-iPad73",
                                      "iPad74-iPad74",
                                      "iPhone8-iPhone8",
                                      "iPhone8Plus-iPhone8Plus",
                                      "iPhoneX-iPhoneX" ],
                "advisories": [ "Infrequent/Mild Sexual Content and Nudity" ],
                "trackCensoredName": "WeChat",
                "trackViewUrl": "https://itunes.apple.com/us/app/wechat/id414478124?mt=8&uo=4",
                "contentAdvisoryRating": "12+",
                "averageUserRatingForCurrentVersion": 4,
                "languageCodesISO2A": [ "AR",
                                        "ZH",
                                        "EN",
                                        "FR",
                                        "DE",
                                        "ID",
                                        "IT",
                                        "JA",
                                        "KO",
                                        "MS",
                                        "PT",
                                        "RU",
                                        "ZH",
                                        "ES",
                                        "TH",
                                        "ZH",
                                        "TR",
                                        "VI" ],
                "fileSizeBytes": "255824896",
                "sellerUrl": "http://www.wechat.com",
                "userRatingCountForCurrentVersion": 135,
                "trackContentRating": "12+",
                "primaryGenreName": "Social Networking",
                "primaryGenreId": 6005,
                "releaseNotes": "Latest Updates:\n- Hide articles or webpages and expand them later to view again\n\nRecent Updates:\n- Save a Moments post as a draft for later editing",
                "releaseDate": "2011-01-21T01:32:15Z",
                "currency": "USD",
                "wrapperType": "software",
                "version": "6.6.7",
                "artistId": 614694882,
                "artistName": "WeChat",
                "genres": [ "Social Networking",
                            "Productivity" ],
                "price": 0,
                "description": "WeChat is a messaging and calling app that allows you to easily connect with family & friends across countries. It's the all-in-one communications app for text (SMS/MMS), voice and video calls, Moments, photo sharing, and games.\n\nWHY USE WECHAT:\n• MULTIMEDIA MESSAGING: Send video, image, text, and voice messages.\n• GROUP CHAT & CALLS: Create group chats with up to 500 people and group video calls up to 9 people. \n• FREE VOICE & VIDEO CALLS: High-quality free calls to anywhere in the world.\n• WECHAT OUT CALLS: Make calls to landlines and mobile around the globe at low rates (in certain regions only).\n• STICKER GALLERY: Hundreds of free, fun, animated stickers to express your feelings from some of your favorite cartoons and movies.\n• MOMENTS: Share your best moments on your personal photo stream.\n• BETTER PRIVACY: WeChat gives you the highest level of control over your privacy. It's the only messaging app to be certified by TRUSTe.\n• MEET NEW FRIENDS: Use “Friend Radar”, “People Nearby” and “Shake” to meet others. \n• REALTIME LOCATION: Instead of having to tell others where you are, just use real-time location sharing.\n• LANGUAGE SUPPORT: Localized in 20 different languages and can translate messages to any language.\n• WeRun-WeChat: Access HealthKit data and challenge friends to beat their scores via the "WeRun-WeChat" Official Account.\n• AND MORE: Desktop app, custom wallpapers, custom notifications, and Official Accounts.",
                "bundleId": "com.tencent.xin",
                "formattedPrice": "Free",
                "isVppDeviceBasedLicensingEnabled": true,
                "genreIds": [ "6005",
                              "6007" ],
                "currentVersionReleaseDate": "2018-05-23T09:59:42Z",
                "sellerName": "Tencent Technology (Shenzhen) Company Limited",
                "minimumOsVersion": "8.0",
                "trackId": 414478124,
                "trackName": "WeChat",
                "averageUserRating": 4.5,
                "userRatingCount": 77410
            }]
    }
    

    通过上面的内容可以看出,接口返回的信息很详细,我们挑一些有用的截取出来:

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

    检查App版本更新功能完整实现:

    /// 检查版本更新
    - (void)checkVersion {
        NSURL * url = [NSURL URLWithString:@"http://itunes.apple.com/lookup?id=414478124"];
        [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *results = dataDic[@"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]) {
                    NSString * releaseNotes = response[@"releaseNotes"]; // 新版本更新内容
                    NSString * alertContent = [NSString stringWithFormat:@"%@\n\n是否前往 AppStore 更新版本?",releaseNotes];
                    // 给出提示是否前往 AppStore 更新
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"检测到有版本更新" message:alertContent 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]];
                    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
                }
            }
        }] resume];
    }
    
    /// 判断是否最新版本号(大于或等于为最新)
    - (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;
    }
    

    参考:http://www.coolketang.com/staticIOS/5b0953b4ac502e0062c27f31.html

    版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

    相关文章

      网友评论

      • 雨影:你不知道用这个检测更新会被拒?
        JerryLMJ:@雨影 你有遇到苹果审核明确指出使用这个api拒绝的案例?还是因为检测更新功能被拒,我还没遇到因为使用这个api被拒的情况
        雨影:@MajorLMJ 调用这个api会,有让用户主动调用检测更新的功能也会,15年的时候,大量APP因为检测更新功能被拒,现在审核松了吗?不知道楼主是否用过这个功能上过架
        JerryLMJ:你指的被拒是调用这个api会被拒,还是检查更新这个功能会被拒?
      • 幻想飞:还是要从后台获取一下的 有些版本更新 要让用户强制更新的

      本文标题:iOS 利用iTunes接口检查App版本更新

      本文链接:https://www.haomeiwen.com/subject/edoojftx.html