gpt4 book ai didi

iOS 无法从 iTunes 获取准确的应用程序版本

转载 作者:搜寻专家 更新时间:2023-10-31 22:38:26 24 4
gpt4 key购买 nike

实际上只是想实现一些功能,比如如果 AppStore 上有新版本的应用程序,那么最终用户将收到更新应用程序的通知。

谷歌上有很多答案,但请看下面我的代码。

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)
}
}
}

问题是,如果新版本的应用程序准备好销售,而我打开旧版本的应用程序,则会打开弹出窗口,当我点击更新按钮时,它将重定向到 AppStore。 但在 AppStore 上显示的不是“更新”按钮,而是“打开”按钮

所以我检查了我的代码。

当我在浏览器上手动运行以下 URL 时

http://itunes.apple.com/in/lookup?bundleId=com.mycompany.appname

然后下载 .txt 文件并获得准确的应用程序版本但是在我的代码中 print(a["version"] as?String ?? "") 它返回错误版本号。

我哪里错了?请指正。

NOTE: I also used http://itunes.apple.com/lookup?bundleId=com.mycompany.appname means removed country code from URL but not worked for me.

最佳答案

您的代码看起来是正确的。正如 Coeur 所说,Apple 会逐步推出新的更新,不会立即推出。更新通常在 24 小时后完成。

就我个人而言,我不会采用这种方法。最好从您的服务器获取版本,您可以在应用发布 24 小时后更新该值。你会对它有更多的控制权。假设您的新应用程序有问题。您可以选择不提示您的用户更新到该版本。

小贴士:

  • 确保只显示一次弹出窗口。
  • 如果服务器版本较低,则不显示弹出窗口。另支持“不”提示用户自由更新。

关于iOS 无法从 iTunes 获取准确的应用程序版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com