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.
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.
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.
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.
You have to set ViewController StoryBoardId property as below image.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With