With this shortcode example, I am going to share with you how to navigate to a different ViewController from AppDelegate.swift file.
If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App
The steps are as follows:
- Create an instance of UIStoryboard,
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
Note the name of the storyboard I am using. The “Main” is the default name of your storyboard when you create a project. If you rename your storyboard to a different name, you will have to use a different name then in the line above.
- Instantiate View Controller with Identifier,
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
The “HomeViewController” identifier I am using in the code line above is the identifier of the ViewController I have created on my Storyboard. Use the identifier if your ViewController you want to navigate to.
- Set the Root View Controller of your app’s window to a different one
self.window?.rootViewController = homePage
Now I am setting the rootViewController of my app’s window object to a view controller I need to navigate.
Below is a complete code example of how to go take user from AppDelegate to a different ViewController.
From AppDelegate.swift to a Different ViewController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // Take user to a home page let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let homePage = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController self.window?.rootViewController = homePage return true }
I hope this Swift code example is helpful for you. Checkout the below video courses to learn more about Mobile App Development for iOS platform with Swift.
[raw_html_snippet id=”cookbookpagecoursesheader”]
Unit Testing Swift Mobile App
Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.