iOS (实现检查更新功能时)获取App最新版本号

func getAppVersionNumber() {
    let appId = "app的Id号"
    // 这里要注意,国内App要使用"https://itunes.apple.com/cn/lookup?id="
    // 如果使用"https://itunes.apple.com/lookup?id="这个则获取不到版本号
    let urlStr = String.init(format:"https://itunes.apple.com/cn/lookup?id=%@", appId)
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if let data = data {
            do {
                if let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any],
                   let results = jsonObject["results"] as? [Any],
                   let firstResult = results.first as? [String: Any],
                   let version = firstResult["version"] as? String {
                    print("Latest version: \(version)")
                }
            } catch {
                print("Error parsing JSON: \(error)")
            }
        }
    }.resume()
}
posted @ 2024-05-18 16:23  幕布_w  阅读(13)  评论(0编辑  收藏  举报