如何将旧版iOS应用程序强制替换为AppStore上的新版本

时间:2017-10-24 09:41:31

标签: swift

如果您使用的是旧版本,则此代码返回true;如果您使用的是AppStore上提供的最新版本,则返回false。如果它的返回真实,我应该如何替换旧版本的应用程序。

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
}

1 个答案:

答案 0 :(得分:0)

最好的方法是告诉用户通过提醒可以获得更新!我们无法通过app

从appstore逐步更新您的版本

我不建议使用此功能,但您可以通过类似此处的警报处理程序从应用程序打开appstore

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

                        }))