如果您使用的是旧版本,则此代码返回 true;如果您使用的是 AppStore 上提供的最新版本,则此代码返回 false。如果它返回 true 那么我应该如何替换旧版本的应用程序。
func appUpdateAvailable() -> Bool
{
let bundleIdentifier = Bundle.main.infoDictionary?["CFBundleIdentifier"]
let identifier = bundleIdentifier as! String
// let storeInfoURL: String = "http://itunes.apple.com/lookup?bundleId=com.xipetech.ecoTrak"
let storeInfoURL: String = "http://itunes.apple.com/lookup?bundleId=\(identifier)"
var upgradeAvailable = false
// Get the main bundle of the app so that we can determine the app's version number
let bundle = Bundle.main
if let infoDictionary = bundle.infoDictionary {
// The URL for this app on the iTunes store uses the Apple ID for the This never changes, so it is a constant
let urlOnAppStore = NSURL(string: storeInfoURL)
if let dataInJSON = NSData(contentsOf: urlOnAppStore! as URL) {
// Try to deserialize the JSON that we got
if let dict: NSDictionary = try! JSONSerialization.jsonObject(with: dataInJSON as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: AnyObject] as NSDictionary? {
if let results:NSArray = dict["results"] as? NSArray {
if let version = ((results[0] as! NSDictionary).value(forKey: "version")!) as? String {
// Get the version number of the current version installed on device
if let currentVersion = infoDictionary["CFBundleShortVersionString"] as? String {
// Check if they are the same. If not, an upgrade is available.
print("\(version)")
if version != currentVersion {
upgradeAvailable = true
}
}
}
}
}
}
}
print(upgradeAvailable)
return upgradeAvailable
}
最佳答案
最好的方法是通过警报告诉用户有可用更新!我们无法通过应用程序从应用程序商店更新您的版本
我不建议使用它,但您可以通过这样的警报处理程序从应用程序打开应用程序商店
refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Cancel, handler: { (action: UIAlertAction!) in
UIApplication.sharedApplication().openURL(NSURL(string:"https://itunes.apple.com/in/app/*******************")!) //Your appstore URL
}))
关于swift - 如何强制将旧版本iOS应用程序替换为AppStore上可用的新版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46907131/