英文:
Although the application is up to date, it gives an update warning
问题
当登录我的应用程序时,它会显示:“我们有一个新的更新。请更新以继续。”并发出警告。但是应用程序已经是最新版本。如果我点击确定,它会将我重定向到应用商店,但没有更新。
如何解决这个错误?
我已经告诉它要将应用程序版本与应用商店版本进行比较,并根据需要提醒更新,但它仍然在提示。
英文:
When logging in to an app I wrote, it says "We have a new update. Please update to continue." gives a warning. But the app is up to date. If I click OK, it redirects me to the app store, but there is no update.
How do I resolve this error?
I told it to compare the app version with the app store version and give an update warning accordingly, but it keeps giving.
答案1
得分: 1
enum VersionError: Error {
case invalidResponse, invalidBundleInfo
}
@discardableResult
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
}
let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)
let task = URLSession.shared.dataTask(with: request) { (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 lastVersion = result["version"] as? String else {
throw VersionError.invalidResponse
}
completion(lastVersion > currentVersion, nil)
} catch {
completion(nil, error)
}
}
task.resume()
return task
}
Usage:
try? isUpdateAvailable {[self] (update, error) in
if let error = error {
print(error)
} else if update ?? false {
// show alert
}
}
英文:
enum VersionError: Error {
case invalidResponse, invalidBundleInfo
}
@discardableResult
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
}
let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)
let task = URLSession.shared.dataTask(with: request) { (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 lastVersion = result["version"] as? String else {
throw VersionError.invalidResponse
}
completion(lastVersion > currentVersion, nil)
} catch {
completion(nil, error)
}
}
task.resume()
return task
}
Usage:
try? isUpdateAvailable {[self] (update, error) in
if let error = error {
print(error)
} else if update ?? false {
// show alert
}
}
I am using the same structure code and its is working fine, here is the reference link -> https://stackoverflow.com/questions/6256748/check-if-my-app-has-a-new-version-on-appstore
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论