iTunes中获取App版本信息--Swift

App版本检测更新

请求URL: let appUrl = URL.init(string: "https://itunes.apple.com/lookup?id=" + appId)

结果:
{
 "resultCount":0,
 "results": []
}

需要在com/后面拼cn/,表明是那个国家。

请求URL: let appUrl = URL.init(string: "https://itunes.apple.com/cn/lookup?id=" + appId)

结果:
{
    "resultCount": 1,
    "results": [{
        "artistViewUrl": "https://itunes.apple.com/cn/developer/æ±�è��å¤�é£�ç½�ç»�ç§�æ��æ��é��å
¬å�¸/id1370226171?uo=4",
        "artworkUrl60": "https://is2-ssl.mzstatic.com/image/thumb/Purple115/v4/6c/c3/f3/6cc3f3bf-9de6-61e8-73a0-6a6834690bcb/source/60x60bb.jpg",
        "artworkUrl100": "https://is2-ssl.mzstatic.com/image/thumb/Purple115/v4/6c/c3/f3/6cc3f3bf-9de6-61e8-73a0-6a6834690bcb/source/100x100bb.jpg",
        "ipadScreenshotUrls": [],
        "appletvScreenshotUrls": [],
        "artworkUrl512": "https://is2-ssl.mzstatic.com/image/thumb/Purple115/v4/6c/c3/f3/6cc3f3bf-9de6-61e8-73a0-6a6834690bcb/source/512x512bb.jpg",
        "screenshotUrls": ["https://is1-ssl.mzstatic.com/image/thumb/Purple115/v4/36/0e/41/360e41d2-f0f7-f429-415a-b67fc0c62e3c/source/392x696bb.jpg", "https://is5-ssl.mzstatic.com/image/thumb/Purple115/v4/ff/c9/f0/ffc9f021-f051-4e9b-66fa-818387627808/source/392x696bb.jpg", "https://is2-ssl.mzstatic.com/image/thumb/Purple115/v4/bf/9f/08/bf9f08e5-7cba-dedd-c159-167992cb3ced/source/392x696bb.jpg", "https://is1-ssl.mzstatic.com/image/thumb/Purple115/v4/25/a7/ba/25a7ba29-583d-eb90-7179-d1346ebcfe73/source/392x696bb.jpg", "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/7a/32/f2/7a32f22c-1d22-2259-f19a-4e98b3dca4b1/source/392x696bb.jpg"],
        "isGameCenterEnabled": false,
        "advisories": [],
        "kind": "software",
        "features": [],
        "supportedDevices": ["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"],
        "trackCensoredName": "商户助手-e融通",
        "trackViewUrl": "https://itunes.apple.com/cn/app/����-e��/id1370226172?mt=8&uo=4",
        "contentAdvisoryRating": "4+",
        "languageCodesISO2A": ["ZH"],
        "fileSizeBytes": "27520000",
        "trackContentRating": "4+",
        "sellerName": "Jiangsu Dooffe Network Technology Co., Ltd.",
        "primaryGenreName": "Utilities",
        "primaryGenreId": 6002,
        "currency": "CNY",
        "wrapperType": "software",
        "version": "1.0.1",
        "description": "我们的app是面向我们的支付系统线下拓展的收款商户。我们的收款商户提出,如果收款时,手机上能够收到通知,最好是能够播报语音的收款提示,可以为他们的工作提供巨大的便捷。",
        "artistId":"artistId",
        "artistName": "江苏多飞网络科技有限公司",
        "genres": ["工具"],
        "price": 0.00,
        "bundleId": "com.dooffe.dooffeEpay",
        "isVppDeviceBasedLicensingEnabled": true,
        "releaseDate": "2018-05-13T11:59:09Z",
        "minimumOsVersion": "10.0",
        "trackId":"trackId",
        "trackName": "商户助手-e融通",
        "formattedPrice": "免费",
        "genreIds": ["6002"],
        "currentVersionReleaseDate": "2018-05-31T18:55:25Z",
        "releaseNotes": "实时推送到账,语音播报功能"
    }]
}

swift 代码封装:

//
//  HHCheckVersionManager.swift
//  HelperApp
//
//  Created by JunQiang on 2018/3/16.
//  Copyright © 2018年 多飞. All rights reserved.
//

import Foundation

class HHCheckVersionManager: NSObject {
    
    /// app版本更新检测
    ///
    /// - Parameter appId: apple ID - 开发者帐号对应app处获取
    init(appId:String) {
        super.init()
        
        //获取appstore上的最新版本号
        let appUrl = URL.init(string: "https://itunes.apple.com/cn/lookup?id=" + appId)
        let appMsg = try? String.init(contentsOf: appUrl!, encoding: .utf8)
        let appMsgDict:NSDictionary = getDictFromString(jString: appMsg!)
        let appResultsArray:NSArray = (appMsgDict["results"] as? NSArray)!
      
        let appResultsDict:NSDictionary = appResultsArray.lastObject as! NSDictionary
        let appStoreVersion:String = appResultsDict["version"] as! String
        let appStoreVersion_Float:Double = CFStringGetDoubleValue(appStoreVersion as CFString)// Float(appStoreVersion)!

        //获取当前手机安装使用的版本号
        let localVersion:String = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        let localVersion_Float:Double = CFStringGetDoubleValue(localVersion as CFString)// Float(localVersion)!

        //用户是否设置不再提示
        let userDefaults = UserDefaults.standard
        let res = userDefaults.bool(forKey: "NO_ALERt_AGAIN")
        //appstore上的版本号大于本地版本号 - 说明有更新
        if appStoreVersion_Float > localVersion_Float && !res {
             let alertC = UIAlertController.init(title: "版本更新了", message: "是否前往更新", preferredStyle: .alert)
             let yesAction = UIAlertAction.init(title: "去更新", style: .default, handler: { (handler) in
                  self.updateApp(appId:appId)
             })
             let noAction = UIAlertAction.init(title: "下次再说", style: .cancel, handler: nil)
                let cancelAction = UIAlertAction.init(title: "不再提示", style: .default, handler: { (handler) in
                    self.noAlertAgain()
                })
                alertC.addAction(yesAction)
                alertC.addAction(noAction)
                alertC.addAction(cancelAction)
                UIApplication.shared.keyWindow?.rootViewController?.present(alertC, animated: true, completion: nil)
            }
            else {
                let alertC = UIAlertController.init(title: "版本检测", message: "App已经是最新版本", preferredStyle: .alert)
                let yesAction = UIAlertAction.init(title: "我知道了~", style: .default, handler: { (handler) in
                    
                })
                alertC.addAction(yesAction)
                UIApplication.shared.keyWindow?.rootViewController?.present(alertC, animated: true, completion: nil)
            }
    }
    
    //去更新
    func updateApp(appId:String) {
        let updateUrl:URL = URL.init(string: "https://itunes.apple.com/cn/app/id" + appId)!
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(updateUrl, options: [:], completionHandler: nil)
        } else {
            // Fallback on earlier versions
            UIApplication.shared.openURL(updateUrl)
        }
    }
    
    //不再提示
    func noAlertAgain() {
        let userDefaults = UserDefaults.standard
        userDefaults.set(true, forKey: "NO_ALERt_AGAIN")
        userDefaults.synchronize()
    }
    
    //JSONString转字典
    func getDictFromString(jString:String) -> NSDictionary {
        let jsonData:Data = jString.data(using: .utf8)!
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
    
}

使用方式: _ = HHCheckVersionManager(appId: kAppId)

大神掠过,欢迎指出问题~~~

你可能感兴趣的:(iTunes中获取App版本信息--Swift)