尽管应用程序已经是最新版本,但它会显示更新警告。

huangapple go评论99阅读模式
英文:

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?

Update Alert

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

huangapple
  • 本文由 发表于 2023年7月6日 19:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定