2017-06-19 59 views
1

我爲我的應用程序強制更新,但是當我回去時不更新應用AlertController消失,因爲它可以固定與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) 

} 

回答

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

讓高層視圖控制器,並提示更新顯示您的警報。

+0

謝謝,一切正常,但第二個更快 – Vasya2014

相關問題