使用AlertController强制更新应用程序

时间:2017-06-19 12:59:56

标签: ios swift url alert uialertcontroller

我为我的应用程序强制更新,但当我返回而没有更新应用程序时AlertController消失,因为它可以修复

如果您知道进行强制更新的其他选项,请编写

感谢您的帮助

   func version(){

    if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
        self.labelVersion = version
        dowloand()
    }

}

func dowloand(){
...JSON request

    if labelVersion == JsonVersion{
        showTopLevelAlert()
    } else {

    }
}


func showTopLevelAlert() {
    let alertController = UIAlertController (title: "Доступно обновление", message: "Для продолжения работы с приложением необходимо обновиться.", preferredStyle: .alert)

    let Update = UIAlertAction(title: "Обновить", style: .default, handler:{(action) in

        let url = URL(string: "http://appstore.com/")!
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }

    })


    alertController.addAction(Update)



    let alertWindow = UIWindow(frame: UIScreen.main.bounds)

    alertWindow.rootViewController = UIViewController()
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    alertWindow.makeKeyAndVisible()

    alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)

}

1 个答案:

答案 0 :(得分:2)

applicationDidBecomeActive中查看您的应用程序版本。

然后发送通知,如:

 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "AppUpdateRequired"), object: nil)

在视图控制器中捕获此通知,以显示带有更新提示的警报。

或者更动态的方式是使用此扩展程序:

extension UIApplication {
  class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        let moreNavigationController = tab.moreNavigationController

        if let top = moreNavigationController.topViewController where top.view.window != nil {
            return topViewController(top)
        } else if let selected = tab.selectedViewController {
            return topViewController(selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(presented)
    }
    return base
  }
}

获取顶视图控制器并显示警报并提示更新。

相关问题