gpt4 book ai didi

ios - 检查应用程序更新时读取 json 错误

转载 作者:行者123 更新时间:2023-11-30 11:46:07 25 4
gpt4 key购买 nike

我使用以下代码来检查更新是否可用。但它给了我错误的版本号。因此,似乎有更新可用,但实际上没有。

enum VersionError: Error {
case invalidResponse, invalidBundleInfo
}

static func isUpdateAvailable() throws -> Bool {
guard let info = Bundle.main.infoDictionary,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let identifier = info["CFBundleIdentifier"] as? String,
let url = URL(string: "http://itunes.apple.com/tr/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
let data = try Data(contentsOf: url)
guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else {
throw VersionError.invalidResponse
}
if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String {

print("version: \(version)") //writes 1.1
print("currentVersion: \(currentVersion)") //writes 1.1.1
return version != currentVersion
}
throw VersionError.invalidResponse
}

我手动下载了该文件,版本号是1.1.1。本来应该如此。但代码给了我 1.1。我不知道出了什么问题。

顺便说一句,更新今天刚刚发布。我认为这应该与此无关。

网址:http://itunes.apple.com/tr/lookup?bundleId=com.sahin.lingustica

最佳答案

在您的方法中,比较两个版本字符串时存在比较问题,请使用此

version.compare(currentVersion, options: .numeric) == .orderedDescending ? true : false

我改变了你的方法试试这个

static func isUpdateAvailable() throws -> Bool {
guard let info = Bundle.main.infoDictionary,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let identifier = info["CFBundleIdentifier"] as? String,
let url = URL(string: "http://itunes.apple.com/tr/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
let data = try Data(contentsOf: url)
guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else {
throw VersionError.invalidResponse
}
if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String {

print("version: \(version)") //writes 1.1
print("currentVersion: \(currentVersion)") //writes 1.1.1
rreturn version.compare(currentVersion, options: .numeric) == .orderedDescending ? true : false
}
throw VersionError.invalidResponse
}

关于ios - 检查应用程序更新时读取 json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com