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

标签 swift

如果您使用的是旧版本,则此代码返回 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/

相关文章:

ios - Swift:实例化 View Controller 时加载 XIB

ios - 在另一个类中调用函数的最佳方式是什么? ( swift )

swift - 如何导出使用 SecKeyGeneratePair 生成的公钥 <SecKey> 以在服务器上使用?

ios - 使导航 Controller 的largeTitleDisplayMode对特定的scrollViews使用react

ios - swift:在导航栏中设置后退按钮图像

swift - 在 Swift 中编码/解码类的重要属性的方法

objective-c - 在 Swift 中使用一个返回 Optional 或 Throw 的 Objective-C 函数

swift - 几秒钟后添加一个 subview

ios - 横向模式下的 UIImagePicker IOS 8

ios - 如何计算 MKMapView 中可见区域的半径?