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 @   幕布_w  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示