AppDelegate, rootViewController and presentViewController

前端 未结 6 2107
南方客
南方客 2020-11-27 15:38

I\'m doing the Facebook integration tutorial, I want to show my MainViewViewController if the user has a valid token for the current state otherwise I want to show LoginView

6条回答
  •  暖寄归人
    2020-11-27 16:28

    Stepan Generalov's answer was the right one for me in Swift 3!!!
    Of course with the new syntax etc. so I'll copy it in here:

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewController(withIdentifier: "MainApp") as! ViewController
    
    var topRootViewController: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
    while((topRootViewController.presentedViewController) != nil){
        topRootViewController = topRootViewController.presentedViewController!
    }
    topRootViewController.present(vc, animated: true, completion: nil)
    

    "MainApp" is my main view controller's identifier in this case.

    I know there are other ways but if you need to have different URL schemes for opening different parts of your App, you must handle it in AppDelegate so this is perfect because in the

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {}
    

    method, you can just check what url is as a String and then decide if you execute the above written code or maybe a similar one with a different identifier for an other view controller (withIdentifier)

提交回复
热议问题