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

通常我们在做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技术博客的原创作品 ,转载时必须注明出处及相应链接!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,347评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,435评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,509评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,611评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,837评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,987评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,730评论 0 267
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,194评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,525评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,664评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,334评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,944评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,764评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,997评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,389评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,554评论 2 349

推荐阅读更多精彩内容