I am currently checking my app version. My apps are notified if there is a new version and should the App Store screen, press the OK. I am checking the app version to do it, but it always shows an error.
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 IXError.invalidBundleInfo
}
Log.Debug(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 IXError.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 IXError.invalidResponse
}
completion(version != currentVersion, nil)
} catch {
completion(nil, error)
}
}
task.resume()
return task
}
Usage
_ = try? isUpdateAvailable { (update, error) in
if let error = error {
Log.Error(error)
} else if let update = update {
Log.Info(update)
}
}
Is this because my app doesn't have an app store?
- If I have an app store, What response can I get to know if I have a version to update?
- How can I go to the App Store?
Please help me a lot.