Их так много ответы в Google, но посмотрите мой код ниже.
Код: Выделить всё
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/in/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
print(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 VersionError.invalidResponse }
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]
if let a = ((json?["results"] as? [Any])?.first) as? [String: Any] {
print(a["version"] as? String ?? "")
}
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
}
Код: Выделить всё
_ = try? appDelegate.isUpdateAvailable { (update, error) in
if let error = error {
print(error)
} else if let update = update {
if update {
let alert = UIAlertController(title: "New Version Available", message: "New version of application is available please update now!", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Update", style: .default, handler: { (actionOk) in
if let url = URL(string: "itms-apps://itunes.apple.com/app/idXXXXXXXXX"),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:])
}
}))
self.present(alert, animated: true, completion: nil)
}
}
}
Поэтому я проверил свой код.
Когда я вручную запускаю URL-адрес в браузере
Код: Выделить всё
http://itunes.apple.com/in/lookup?bundleId=com.mycompany.appname
Где я ошибаюсь? Пожалуйста, поправьте меня.
ПРИМЕЧАНИЕ. Я также использовал http://itunes.apple.com/lookup?bundleId ... ny.appname. означает удаление кода страны из URL, но у меня это не сработало.
Подробнее здесь: https://stackoverflow.com/questions/522 ... rom-itunes