【发布时间】:2019-10-22 21:05:46
【问题描述】:
我通过请求 http://itunes.apple.com/lookup?bundleId=(identifier) 从 appStore 获取我的应用程序的信息字典并收到 1.8.2,但在 appStore 版本是 1.8.3,如果我在 iPhone 上设置应用程序 - 有关应用程序版本的本地信息是正确的1.8.3,为什么appStore请求的版本号错误?
func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask {
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/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
print("currentVersion: \(currentVersion)")
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
if let error = error { throw error }
guard let data = data else { throw VersionError.invalidResponse }
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]
guard let result = (json?["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String else {
throw VersionError.invalidResponse
}
print("AppStore version: \(version)")
completion(version != currentVersion, nil)
} catch {
completion(nil, error)
}
}
task.resume()
return task
}
// 使用它我叫:
_ = try? isUpdateAvailable { (update, error) in
if let error = error {
print(error)
} else if let update = update {
print(update)
}
}
【问题讨论】:
-
自发布之日起24小时内继续返回当前版本应用程序版本号不正确,但是是什么原因以及如何获取正确的版本号呢?