How to navigate to view controller on push notification - ios

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)
}

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)
}

Launch ViewController from AppDelegate, tabbar missing from bottom

I am developing my first app, where I am trying to manage a session in my app, where I am trying to check if user has logged in recently. If user has logged in recently, then I want to skip login page, and move him to next page.
Here what I am doing, however I am unable to proceed forward
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewControllerB = mainStoryboard.instantiateViewControllerWithIdentifier("account") as! AccountDetails
let navController = UINavigationController(rootViewController: viewControllerB)
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
appDelegate.window?.rootViewController = viewControllerB
let vc = self.window?.rootViewController
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
return true
}
I am able to get to the desired view,but the Tabbar on that view is missng. I want to restore the same.
Second Screen (My Account which I want to show)
class AccountDetails: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Initialize Tab Bar Item
tabBarItem = UITabBarItem(title: "Account Details", image: UIImage(named: "Account.png"), tag: 1)
}
}
Replace the line self.window?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
with
self.window?.rootViewController = navController
Change window's rootViewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if(User loggin last time) {
let yourTargetViewController = UIViewController()
yourTargetViewController.view.backgroundColor = UIColor.redColor()
self.window?.rootViewController = yourTargetViewController
}
return true
}
This is the working solution
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewControllerB = mainStoryboard.instantiateViewControllerWithIdentifier("tabbar") as! UITabBarController
let navController = UINavigationController(rootViewController: viewControllerB)
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
appDelegate.window?.rootViewController = viewControllerB
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
//tabbar is the storyboard ID of tabbarcontroller

Switch View in AppDelegate (Swift)

I need to set the starting View of my App depending on a user is registrated or not.
This will be done in AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if(isUserRegistrated() == true){
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("isRegistrated")
self.showViewController(vc as UIViewController, sender: vc)
}
else{
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("isNOTRegistrated")
self.showViewController(vc as UIViewController, sender: vc)
}
}
At this point i got the error:
"Value of type AppDelegate has no member of storyboard"
Any conclusions ?
Sample:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

From AppDelegate didreceiveremotenotification navigate to a certain viewcontrolller - swift

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

Resources