Login required for access
In order to watch this lesson, you'll need to login to the website.
Hijacking Root View Controller from AppDelegate
Audible Login Walkthrough
An important topic today that we'll be going over is how to present our login component as our initial view controller. This is dependent on the condition that our user is already logged in or not. To do this efficiently, we're going to take over the initial root view of our UIWindow and present our own UINavigationController that either holds our Home screen or our Login screen. This is a very interesting technique that you can also adopt for you app.
!codebreak <div class='filename'>AppDelegate.swift</div> !codebreak !syntax-highlight class AppDelegate: UIResponder, UIApplicationDelegate { //... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() window?.rootViewController = MainNavigationController() return true } //... } !codebreak <div class='filename'>MainNavigationController.swift</div> !codebreak !syntax-highlight class MainNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white if isLoggedIn() { //assume user is logged in let homeController = HomeController() viewControllers = [homeController] } else { perform(#selector(showLoginController), with: nil, afterDelay: 0.01) } } fileprivate func isLoggedIn() -> Bool { return true } func showLoginController() { let loginController = LoginController() present(loginController, animated: true, completion: { //perhaps we'll do something here later }) } } !codebreak <div class='filename'>HomeController.swift</div> !codebreak !syntax-highlight class HomeController: UIViewController { override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "We're logged in" let imageView = UIImageView(image: UIImage(named: "home")) view.addSubview(imageView) _ = imageView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 64, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) } }

Comments (2)
5 years ago
8 years ago
8 years ago
7 years ago
HELP & SUPPORT