How to push viewcontroller from appdelegate in storyboard inside navigation controller - ios

I am using SWRevealViewController in my project, and I want to open a particular controller when the app receives a notification. I have tried so many solutions but nothing works.
I follow this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard. My storyboard is designed as below:
When the application receives a notification I want to load Photo view controller within its navigation controller. I tried with the following code in the AppDelegate:
UIStoryboard *st = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: #"photoView"];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: #"menuController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] init];
mainRevealController.rearViewController = rearViewController;
mainRevealController.frontViewController= frontNavigationController;
self.window.rootViewController =nil;
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
This works, but creates a new Navigtion controller, and what I need is to use the one already defined in the storyboard, since it has specific properties.
Any idea?
Thanks

actually SwLRevalViewController Taken the ownership of Root, so do like this
in Appdelegate.m
//initially navigate to your Main controller on SWL
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSString *alert = userInfo[#"aps"][#"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:#"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
self.window.rootViewController = main;
}
//it automatically redirect to your root controller
on that main view controller ViewDidLoad check / Navigate to your photo view controller
NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:#"itemType"];
if ([gettypeofItem isEqualToString:#"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:#"yourPhotoViewController"];
[self.navigationController pushViewController:ivc animated:YES];
}
else
{
// do nothing
}

You can use following method
NSString *storyBoardName=#"Main_iPad";
if (isPhone)
storyBoardName=#"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];
PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:#"playVideoUsingSockets"];
float disptachTime=2.0;
if (value)
{
disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:controller animated:YES completion:nil];

please try this it may help you. in this when you click on notification this method us called first insted for appdelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}

Related

go to specific post/comment/profile after click push notifications in iOS 9

I have tried many code in my didReceiveRemoteNotification function in appdelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Code
}
but didn`t work as expected.
With this code, doesn`t navigate to my profile view controller after I click the notification:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:controller animated:YES];
I have tried this also but didn`t navigate to the profile controller
[self.window.rootViewController presentViewController:controller animated:YES completion:NULL];
Finally, I try this code, but it is weird because it doesn't have back button (I think the cause is the profile controller become the initial view).
self.window = [[UIWindow alloc]
initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
Any one have solution?
edit:
I think we can not set our target View controller to become a Root View Controller, because when we click notifications in other apps, we go to main screen first, then go to the specific view controller immediately
Try this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:controller];
self.window.rootViewController = navController;
`

Open specific view controller on click of local notification, if app is in foreground?

I am trying to open a specific view controller on click of local notification, if app is in background, i am unable to do that because i don't have [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; in applicationDidBecomeActive method. Please help if you can, Thanks in advance.
do like
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
yourViewController *obj=[storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerStoryboardID"];
// [self.window.rootViewController.navigationController pushViewController:obj animated:YES];
self.window.rootViewController = obj;
Update
- (void)applicationDidBecomeActive:(UIApplication *)application {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MapViewController *obj=[storyboard instantiateViewControllerWithIdentifier:#"Map"];
// [self.window.rootViewController.navigationController pushViewController:obj animated:YES];
self.window.rootViewController = obj;
}
Update1
you can get the localnotification action in here
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
for complete tutorial see this

Pushing view controller from AppDelegate

I am just beginning to learn how to implement Push notifications with iOS and Parse.
At the moment, I can send push notifications from the Parse dashboard to the app,the notification is received, as I can see in the Xcode console, but now my issue is that I am not able to open a view controller from the AppDelegate.
This is my code:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
// Create empty photo object
NSString *photoId = [userInfo objectForKey:#"p"];
NSLog(#"P=%#",photoId);
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *controlador = [[UINavigationController alloc]init];
[controlador pushViewController:targetViewController animated:YES];
}
As I said, the console show the log NSLog(#"P=%#",photoId) , the notification is received, but the view controller is not shown, any warning or error is shown.
Any help is welcome.
I have solved my problem like this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
Aceptado *controller = (Aceptado*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"servicio_aceptado"];
controller.id_not =_id_not;
controller.idemployee = _idemployee;
controller.fullname = _fullname;
controller.phone = _phone;
controller.photo = _photo;
controller.dni = _dni;
controller.licenseplate = _licenseplate;
controller.model = _model;
controller.color = _color;
controller.latitude = _latitude;
controller.longitude = _longitude;
self.window.rootViewController = controller;
Now I can pass values to the target view controller from the AppDelegate file, keeping the SWRevealViewController structure.
Thank to all of you for your proposals.
try this code.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *navVc=(UINavigationController *) self.window.rootViewController;
[navVc pushViewController: targetViewController animated:YES];
may help you.
Try this once.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Slider1ViewController *viewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"Slider1ViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[navController setViewControllers: #[viewcontroller] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
You should not create new NavigationController, but use one existing. Usually it is rootViewController property of your key window.
Try this:
UINavigationController *controlador = self.window.rootViewController;
If you want to display your targetViewController in SWRevealViewController you may change your code as this:
SWRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador pushFrontViewController:targetViewController animated:YES];
If you need display targetViewController over SWRevealViewController, use this:
WRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador presentViewController:targetViewController
animated:YES
completion:nil];
If you need just replace SWRevealViewController with targetViewController use this:
self.window.rootviewController=targetViewController;
But don't forget save your SWRevealViewController in some another property.
Try this:
SomePageViewController *someVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeVC"];
self.navController = [[UINavigationController alloc] initWithRootViewController:someVC];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
If you want to push a view controller into navigation stack from appDelegate you need to cast your rootView controller as UINavigation Controller

How to get UINavigation controller from Tabbar controller and push a specific view

I am trying to implement didReceiveRemoteNotification method. When I receive a push notification I want to show a specific view. My app is using UITabbarController as root view controller and UINavigationController as its children:
and here is my appdelegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UINavigationController *navController = (UINavigationController *)[[((UITabBarController *)self.window.rootViewController) viewControllers] objectAtIndex:2];
NSString * storyboardName = #"others";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessAccountView"];
[navController pushViewController:vc animated:YES];
[PFPush handlePush:userInfo];
}
Unfortunately the code does not work. It works if I change [navController pushViewController:vc animated:YES] to [navController presentViewController:vc animated:YES], but I will lose my navigation bar.
Does anyone have an idea how to do this?
It's a stepwise solution.
First check if the tab on which you want to push the view controller is already selected. If not, use setSelectedIndex or setSelectedViewController to select that tab first.
Next, get the rootViewController (your UINavigationController) of the tabBarController's selected view controller.
Next, instantiate the viewController you want to push, and push it on the navController.
Sample code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UITabBarController* tabController = self.window.rootViewController;
if (tabController.selectedIndex != 2) [tabController setSelectedIndex:2];
UINavigationController *navController = tabController.selectedViewController;
NSString * storyboardName = #"others";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessAccountView"];
[navController pushViewController:vc animated:YES];
[PFPush handlePush:userInfo];
}
Follow this tutorial (Storyboard Tutorial: Create Tab Bar Controller and Web View)....
http://www.appcoda.com/storyboard-tutorial-create-tab-bar-controller-and-web-view/

iOS pushNotice redirect - stroyboard

I would like to redirect to the specific view when touching the push notice alarm outside the app. I've write some code like this...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if (application.applicationState != UIApplicationStateActive){
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ChatViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"ChatView"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
}
It did redirect to the chatViewController, but only chatViewController have been launch. Do I have to write down the whole Controller structure of my app, or there are other ways to do so?
The structure of my app is a UITabBarViewController with four tab...
tab1 > UINavigationController > UICollectionViewController > UIViewController
tab2 > UINavigationController > UICollectionViewController
tab3 > UINavigationController > UITableView > UITableView(chatViewController)
tab4 > UITableView
I believe you should create the stack of view-controllers.
First you say to push to the first table view controller, mentioning it a push coming. You can invoke a push constructor. Below is the code in UITableViewController*.
tab3 > UINavigationController > UITableViewController.
- (id)initWithPush:(Chart*)chart {
self = [self initWithNibName:<#(NSString *)#> bundle:<#(NSBundle *)#>];
if (self) {
[self pushToChart:chart];
}
return self;
}
- (void)pushToChart:(Chart*)chart {
// add the code to push to next Chart-View-Controller
}
I found how to get viewController in storyboard. So I can directly do this...
UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
UINavigationController *nav = tab.viewControllers[0];
tab.selectedViewController = nav;
myViewController *vc = (ChatListViewController *)nav.topViewController;
[vc.navigationController pushViewController:vc animated:YES];

Resources