From AppDelegate didreceiveremotenotification navigate to a certain viewcontrolller - swift - ios

I have my TabBarController as my initial view (storyboard identity Main) (Class UITabBarController)
Then a NavigationController (storyboard id = ViewController) (Class UINavigationController).
Then my ViewController (storyboard id = View) (Class ViewController)
Then my Details (storyboard id = StoryDetails) (Class Details)
I would like to navigate from AppDelegate.swift to the Details.swift uiviewcontroller.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let tabViewController = storyboard.instantiateViewControllerWithIdentifier("StoryDetails") as? UITabBarController {
window!.rootViewController!.presentViewController(tabViewController, animated: true, completion: nil)
}
But this is not working
Help would be really appreciated! (:
Also
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let detail = storyboard.instantiateViewControllerWithIdentifier("StoryDetails")
self.window?.rootViewController!.presentViewController(detail, animated: true, completion: nil)
This code brings up the view, but there is no navigation controller or tab bar controller

Related

Navigate to a Specific view on click on a push notification with Reveal

I want to navigate to a specific viewcontroller when the I click on a push notification.
I have written this code in my didReceiveRemoteNotification method.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC
let naviVC:UINavigationController? = self.window?.rootViewController?.revealViewController().frontViewController as? UINavigationController
naviVC?.pushViewController(vc, animated: true)
}
But it's giving me this error which crashes the app.
fatal error: unexpectedly found nil while unwrapping an Optional value
There are many posts with navigation controller or just presenting the viewcontroller. But I want to navigate to the specific view with reveal.
Any help would be highly appreciated.
here is a another solution for this Try this:-
First Option :-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier:
"deals") as! FYIDealsVC
// setup revelview controller and root with window
self.window?.rootViewController = //object of revelViewController
self.window?.makeKeyAndVisible()
Second option :-
// in Landing Screen from where you can easily navigate to the target
viewcontroller :-
in Landing VC:-
viewdidload:-
NotificationCenter.default.addObserver(self, selector:
#selector(self.navigationForNotification(notification:)), name:
NSNotification.Name(rawValue:PushNavigationIdentifier), object: nil)
// Selector Method :-
func navigationForNotification(notification:Notification) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier:
"deals") as! FYIDealsVC
self.navigationController?.pushViewController(vc, animated: true)
}
in appDelegate :-
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
NotificationCenter.default.post(name:
NSNotification.Name(PushNavigationIdentifier), object: userInfo)
}
Looks like error happens when you force unwrap FYIDealsVC.
Can you use
let vc = FYIDealsVC()
instead of
let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC

iOS push notification click causes app to crash

I am having an issue with my push notification click. Everytime user clicks on the notifications, the app will crash instead of redirecting user to the specified page.
This part of the code is causing an error "Could not cast value of type 'appname.LaunchScreenController' to 'UINavigationController'"
:
let rootViewController = self.window!.rootViewController as! UINavigationController
And this code will cause fatal error: unexpectedly found nil while unwrapping an Optional value :
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
//receive the notifications
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(vc, animated:true)
}
Thanks in advance
RootViewController is subclass of UIViewController not a UINavigationController You have to handle your null values
change to
let rootViewController = self.window!.rootViewController
I change the code to this and it works just fine now
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let nav = UINavigationController()
nav.pushViewController(vc, animated: true)
}

3D Touch Quick Action Won't work swift 3

I am trying to add 3D touch quick action in my app. I have two viewContollers embedded in navigationContoller. When the user press on the 3D action, it should take them to the add events viewController. But i am getting this error
could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360).
2017-05-02 02:51:36.220324-0400 Morning Star 2[3731:895126] Could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360).
Here is my code
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.krp.addEvent" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! UINavigationController
self.window?.rootViewController?.present(addEventVC, animated: true, completion: nil)
}
}
I tried changing as! UINavigationController to as! AddEventsViewController. It works, but then it won't show the navigation bar on the top on my add viewController as it normally would if you open the app normally.
Here is my main.storyboard
You shouldn't cast AddEventsViewController to UINavigationController, because it isn't UINavigationController, is just subclass of UIViewController. But as I see, your rootViewController is. So you need to cast it to UINavigationController, and then push your AddEventsViewController, instead present, and you will see NavigationBar
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.krp.addEvent" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! AddEventsViewController
if let navigationController = window?.rootViewController as? UINavigationController {
navigationController.pushViewController(addEventVC, animated: true)
}
}
}

Open controller from app delegate

I am trying to open a view controller from app delegate if a push notification is clicked with the code below
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if(application.applicationState==UIApplicationState.Inactive ){
let sdViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("StudentViewController") as! StudentViewController
let navController = UINavigationController(rootViewController: sdViewController)
navController.setViewControllers([sdViewController], animated:true)
self.window?.rootViewController = navController
}
}
The controller opens, however the menu icon to reveal the navigation does not work. How can i make the menu icon reveal the side navigation.
Try this
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(sdViewController, animated: true)

How to navigate to view controller on push notification

I'd like to navigate to a certain view controller after receiving a push notification. After navigation, the navigation stack should work as if the user got to the view manually.
The storyboard: http://www.xpos.nl/xpos/images/common/storyboard.png
In AppDelegate.swift I already have:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("didReceiveRemoteNotification")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController
let navigationController = self.window?.destinationViewController;
navigationController?.pushViewController(destinationViewController, animated: false, completion: nil)
}
But I get an error that destinationViewController is not part of window or if I correct that (trying other answers on stackoverflow), nothing happens.
The destinationViewController is not part of the window because it has not been added, just initialized. Based on the assumption that the navigationViewController is your rootViewController, push to your destinationViewController like this:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("didReceiveRemoteNotification")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController?.pushViewController(destinationViewController, animated: false, completion: nil)
}
Additionally: To push from "Bestellen" to "MessageViewController" and then pop to "Berichten", you need to push all the other viewControllers between those two, too. There is no built in function or algorithm to do that.
Try this
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("didReceiveRemoteNotification")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController
self.window?.rootViewController?.presentViewController(destinationViewController, animated: True, completion:nil)
}

Resources