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)
}
}
Please help me a lot.
Yes, the method you use must be an already published app.
If you use an unpublished app, you will get results = []
Go to the App Store
like this
let appId = "1454358806" // Replace with your appId
let appURL = URL.init(string: "itms-apps://itunes.apple.com/cn/app/id" + appId + "?mt=8") //Replace cn for your current country
UIApplication.shared.open(appURL!, options:[.universalLinksOnly : false]) { (success) in
}
This method will not be very timely, meaning that the application you just released, even if it can be searched in the App store
, but results
will not be updated immediately. Update information will be available after approximately 1 hour, or longer
I have done this via a completion handler
func appStoreVersion(callback: @escaping (Bool,String)->Void) {
let bundleId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
Alamofire.request("https://itunes.apple.com/lookup?bundleId=\(bundleId)").responseJSON { response in
if let json = response.result.value as? NSDictionary, let results = json["results"] as? NSArray, let entry = results.firstObject as? NSDictionary, let appStoreVersion = entry["version"] as? String{
callback(true,appStoreVersion)
}else{
callback(false, "-")
}
}
}
appStoreVersion contains your app version on app store. Remember: When your app goes live on app store, it may take up to 24 hours until you see the latest version. Here is how to use it:
appStoreVersion { (success,version) in
appVersion = version
self.VersionLabel.text = "App version \(appVersion)"
}
You can use the success version to do a different things it version cannot be retrieved. ie you are not connected or. You can check how it is used in this app(Settings Tab):https://apps.apple.com/us/app/group-expenses-light/id1285557503
I have used the completion handler too.
private func getAppInfo(completion: @escaping (AppInfo?, Error?) -> Void) -> URLSessionDataTask? {
guard let identifier = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String,
let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else {
DispatchQueue.main.async {
completion(nil, VersionError.invalidBundleInfo)
}
return nil
}
private func getVersion() {
_ = getAppInfo { info, error in
if let appStoreAppVersion = info?.version {
let new = appStoreAppVersion.components(separatedBy: ".")
if let error = error {
print("error getting app store version: \(error)")
} else {
print(new)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.