Skip to content

Instantly share code, notes, and snippets.

@thecoolwinter
Created October 4, 2024 17:44
Show Gist options
  • Save thecoolwinter/31af9531b614eed8928dae61062a6227 to your computer and use it in GitHub Desktop.
Save thecoolwinter/31af9531b614eed8928dae61062a6227 to your computer and use it in GitHub Desktop.
func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask {
print("\(String(describing: type(of: self))) \(#function)", level: .info)
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 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
}
completion(version != currentVersion, nil)
} catch {
completion(nil, error)
}
}
task.resume()
return task
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment