Created
October 4, 2024 17:44
-
-
Save thecoolwinter/31af9531b614eed8928dae61062a6227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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