Open scene from Push notifications - ios

I have a storyboard like this:
My Storyboard
I need to open the scene shown in the above image when you receive a push notification. I tried this way:
-(void)application:(UIApplication*)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NotificationsViewController *notificationobject = [[NotificationsViewController alloc]init];
[self.navigationController pushViewController:notificationobject animated:YES];
gotNotifcation = YES;
}
But only it opens the NavigationControll black. How can I do?

You have NotificationsViewController in your storyboard but I'm not certain the object you are doing an alloc & init on is the same view controller.
Try doing this instead:
// replace everything between the quotes with the correct name & identifiers
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myStoryboardName" bundle:nil];
NotificationsViewController *vc = (NotificationsViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myNotificationsViewController"];
[self.navigationController pushViewController:notificationObject animated:YES];
gotNotification = YES;
OR, since there appears to be a segue there to get you to your storyboard, try doing:
[self performSegueWithIdentifier:#"SegueIdentifier" sender:self];

Related

How to push viewcontroller from appdelegate in storyboard inside navigation controller

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];
}

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];

Presenting a view for the first time in appDelegate

The goal:
When my app starts up - I need it to display a view before it gets to the "Home" screen. Its a tab bar application and this view is not part of the tabbar.
I am using Storyboards and Xcode 5 - iOS7 only app.
The problem:
I have code that will check if the app is first launch or not. Based on that, I then want to present a one time only view to the user.
What I have tried:
The following code is in the appDelegate of the application as this is where it all starts. I call the following bit of code in there:
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
}
I get compile errors as [self.navigationController..] doesn't exist. Nor does [self.tabbarcontroller...];
This is obvious as I don't have properties setup for these - but how do I go about resolving this and connecting the tab bar to the storyboard?
What am I missing?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if(!isAgreementAccepted)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDD"];
self.window.rootViewController=vc;
}
return YES;
}
If agreement is not accepted set the T&C viewController as rootViewController when user click the accept button then set the TabBarviewController as root.
u can access the widow object through application delegate any where
[[[UIApplication sharedApplication]delegate] window].rootViewController=tabViewController.
Change the rootviewcontroller of window programaticaly
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *aStoryBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UITabBarController *aTabCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"Tab"];
FirstVC *aFirstCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"First"];
if(self.termsHaveBeenAccepted)
self.window.rootViewController=aFirstCtrl;
else
self.window.rootViewController=aTabCtrl;
return YES;
}
This will definitely work I have tested.
If Tabbarcontroller is your root viewcontroller, then using below code will resolve the issue in your appdelegate.
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController presentModalviewcontroller:vc animated:YES completionblock:nil];
}
add your viewcontroller superview of tabbarcontroller as first time else call tabbarcontroller alone

Cannot push viewcontroller from appdelegate

In my window base application I need to navigate to editorview from my appdelegate when i click on the second item of my tabbarcontroller it will shows an actionsheet with three option.
actionsheet works fine, it will shows an actionsheet if you choose the second item from tabbar.
But i need to push to the other view when i choose the first option of my action sheet
i declare my actionsheet in appdelegate.
i already implement the actionsheetdelegate
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
the method is trigerred and it can shows if i do nslog. but it cannot push to the viewcontroller that i want..
here's my code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
DetailViewController *v2 = [[DetailViewController alloc] init];
NSLog(#"PUSH TRIGER");
[self.window.rootViewController.navigationController pushViewController:v2 animated:YES];
}
}
NOTE: I already embed my tabbarcontroller and navigation controller in my storyboard
It is full of bad answer related to this issue.
I hope mine fits your requirements.
1.- Inside of
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
// 1.- First: instantiate the navigationController. Normally the rootviewcontroller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// 2.- Second: Instantiate Storyboard
// It might be tagged MainStoryBoard. Be careful if you've changed it
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
// 3.- Third instantiate the VC you want to navigate
// In my particular case it is called IniciarSliderViewController . Don't forget to import it in the appdelegate. Also pay attention to tagged correctly. In my case MenuSlider
IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"MenuSlider"];
//4.- And last one, you can now push as you usually do in the VC
[navigationController pushViewController:controller animated:YES];
Don't hesitate to ask if you still have problems
Do like this
What did you assign VC for UITabBarController ? I bet UIViewController? Try assign UINavigationController instead of UIViewController
synthesize UINavigationController *navController;
self.navController = [[UINavigationController alloc] init];
SomeViewController *viewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController" bundle:nil];
self.navController.viewControllers = [NSArray arrayWithObject:viewController];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
self.navController, nil]];
Then on your actionsheet
[self.navController pushViewController:v2 animated:YES];
EDIT for storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
self.navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:#"MyNavController"];
Hope that helps
instead of this self.window.rootViewController.navigationController
use UINavigationController object

Resources