Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening view controller from app delegate using swift

I am trying to create a push notification which determines which view to open according to information obtained from the push.

I have managed to get the information from the push, but I am now struggling to get the view to open

Looking at other stack overflow questions I have the following currently:

App Delegate Did finish loading:

     //Extract the notification data     if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {         // Get which page to open         let viewload = notificationPayload["view"] as? NSString         let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)         //Load correct view         if viewload == "circles" {              var viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("Circles") as! UIViewController             self.window?.rootViewController = viewController                          }     } 

Currently this is failing on the var ViewController = self... line.

like image 453
Lister Avatar asked Jun 02 '15 09:06

Lister


People also ask

How do I present a view controller from another view controller?

Using segues in your storyboard is the recommended way to present and dismiss view controllers. A segue is a visual representation of a transition from one view controller to another. A segue starts with an action such as a button tap or table-row selection in the initial view controller.

How do I get to the root view controller in Swift?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.

How do I get Navigationcontroller in Appdelegate?

You make the navigation controller in the AppDelegate in the application:DidFinishLaunchingWithOptions: method. The first two steps are the same as the playground. Make a root view controller, and make a navigation controller from the root controller. You'll notice the top of the Appdelegate the var window.


1 Answers

You have to set ViewController StoryBoardId property as below image.

enter image description here

open viewController using coding as below in swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {           let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)          let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController          self.window = UIWindow(frame: UIScreen.main.bounds)          self.window?.rootViewController = initialViewControlleripad          self.window?.makeKeyAndVisible()           return true     } 

For iOS 13+ (based on an article by dev2qa)
Open SceneDelegate.swift and add following

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {      // If this scene's self.window is nil then set a new UIWindow object to it.     self.window = self.window ?? UIWindow()      // Set this scene's window's background color.     self.window!.backgroundColor = UIColor.red      // Create a ViewController object and set it as the scene's window's root view controller.     self.window!.rootViewController = ViewController()      // Make this scene's window be visible.     self.window!.makeKeyAndVisible()      guard scene is UIWindowScene else { return } } 

There is an open-source navigation utility which attempts to make this easier. Example

like image 95
Kirit Modi Avatar answered Sep 18 '22 13:09

Kirit Modi