Navigating from Appdelegate when push message received - ios

Currently this is how my method loooks
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString *screenNo =[userInfo objectForKey:#"screen"];
}
Based on the screenNo I would like to navigate to different view controllers. But I couldn't do as most of the answers given below.
Reason is that my root view is not navigation control, so I couldn't segue. It crashes the app.
when push message arrives didReceiveRemoteNotification is called and I could see the content of the message too. But it doesn't get navigated using the methods shown here.
[self.window makeKeyAndVisible];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"galleryViewController2"];
[(UINavigationController *)self.window.rootViewController pushViewController:vc animated:YES];
this is the exception
2014-07-21 18:06:53.709 Proitzen Rest[993:60b] -[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270
2014-07-21 18:06:53.712 Proitzen Rest[993:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270'
*** First throw call stack:
(0x2f480fd3 0x3a021ccf 0x2f484967 0x2f483253 0x2f3d27b8 0xff93b 0x31eb3b29 0x31eb37fb 0x31dbb05f 0x31e6d377 0x31d1c6f5 0x31c9555b 0x2f44c2a5 0x2f449c49 0x2f449f8b 0x2f3b4f0f 0x2f3b4cf3 0x342da663 0x31d0016d 0x157e69 0x3a52eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Thanks for your time in advance.

Did you try something like this?
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"galleryViewController2"];
self.window.rootViewController = vc;
Instead of pushing your new controller (it crashes because to push you need a navigation controller) you can replace current controller with the new one.
Please, take in account that you can not pop to the original controller (if you need to get back, you need a navigation)

You're trying to push a UIViewController with a UIViewController. This is not possible. You must have a UINavigationController in your app hierarchy.
You can also just set the rootViewController:
[self.window setRootViewController: newViewController];

make sure you call this method before trying to present any view controller.
[self.window makeKeyAndVisible];

You can't navigate using push and Pop from APPDelegate if you need to navigate from appDelegate to a file, Segues won't help either then You would need to load it first in your window and then make it Visible such as..
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *screenNo =[userInfo objectForKey:#"screen"];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
firstViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:#"firstVC"];
// First item in array is bottom of stack, last item is top.
navController.viewControllers = [NSArray arrayWithObjects:menu, nil];
[self.window makeKeyAndVisible];
}

This is what finally saved me. placed it inside didReceiveRemoteNotification method.
NSLog(#"User wanna navigate");
[self.window makeKeyAndVisible];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"moreTableViewController"];
self.window.rootViewController = vc;
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *evc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"eventsViewController"];
[navController.visibleViewController.navigationController pushViewController:evc animated:YES];

Related

NSInternalInconsistencyException throwing by AppDelegate

I'm trying to open a storyboard from my AppDelegate but it throws me the exception:
2016-10-25 10:26:16.776 momnt[22865:1300106] *** Terminating app due to
uncaught exception 'NSInternalInconsistencyException',
reason: 'Application windows are expected to have a root view
controller at the end of application launch'
Here is what i'm trying to do:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:#"Login" bundle:nil];
UIViewController *mainViewController = [loginStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
I've read that newer versions of XCode requires that all the Windows must have a rootViewController but i've did that.
Debug the application and check if mainViewController is not nil.
Problem seems that you don't have any viewController set as initial viewcontroller in your storyBoard.
Set any viewcontroller you want to initial viewcontroller and your code work fine.
OR
Use instantiateViewControllerWithIdentifier and pass your viewController identifier to which you want as initial viewController.
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"Login" bundle:nil];
UIViewController *mainViewController = [storybord instantiateViewControllerWithIdentifier:#"InitialViewControllerID"];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];

On click events app crashed after navigation of a segue when receiving push notification

My application is almost completed, now i'm working on push notifications. I able to receive push notifications successfully, unfortunately i'm not able to perform segue in Appdelegate. Basically i have two base screens 1.login 2.Tabbar controller i'm handling this in app delegate as shown below.
UIStoryboard* appStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (![self connect])
{
UINavigationController *home=[appStoryboard instantiateViewControllerWithIdentifier:#"loginScreen"];
self.window.rootViewController=home;
}
else
{
//TabbarHome
UITabBarController *home=[appStoryboard instantiateViewControllerWithIdentifier:#"TabbarHome"];
//pushNotificationSeague
self.window.rootViewController=home;
}
when i receive a notification i tried to push using Storyboard ID like
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NotificationViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MyNotification"];
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
[self.navigationController pushViewController:controller animated:YES];
Now navigation is done. But the Click event are making app to crash. Reason i found is the below line, without this line of code app will not navigate. If we i use the below code the allocation of memory issue is there i guess.
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
Please help me with fixing issue. Why the actions in the NotificationViewController are making crash. crash report is
[NotificationViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x7ffdc60a8890
Do not add addSubview to window.
It is not making sense that you are pushing the viewController and also you are adding it to window subview. Just remove the following line
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
Finally it should be
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NotificationViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MyNotification"];
[self.navigationController pushViewController:controller animated:YES];

iOS - navigate From AppDelegate to UITableViewController

I try to navigate to certain ViewController after I get remote Notification
It crashes after this code any help please
My storyboard like this
SWRevealViewController -> NavViewController -> UIViewController
I want to reach this UIViewController
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
// my ViewController I want navigate to UITableViewController call home_tableview
Home_tableView *home =[storyboard instantiateViewControllerWithIdentifier:#"home_view"];
[(UINavigationController *)self.window.rootViewController pushViewController:home animated:NO];
}
The Error I get
2015-01-20 13:04:14.379 SchoolLink[1304:416727] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:(0x25bd749f 0x3338dc8b 0x25af30b3 0x2909d06b 0x11d48f 0x29099d0f 0x29099a7d 0x2909f953 0x2910643d 0x111dbd 0x2930286b 0x292fa54d 0x2c3680d1 0x25b9dd7d 0x25b9d041 0x25b9bb7b 0x25ae93c1 0x25ae91d3 0x2cee70a9 0x290f8fa1 0x12b7ad 0x3390daaf)
libc++abi.dylib: terminating with
I suppose you have an UINavigationController in your storyboard which embeds your view controllers.
If that's the case, your code should work without any issues.
I've tested it in a new project with two view controllers and one navigation controller as the initial view and everything is fine.
If you're certain that you have all the storyboard identifiers right, and that your initial View in Interface Builder is an UINavigationController that embeds all of your ViewControllers, then you have some issues elsewhere, not in the code you've pasted in.
Since your error is related to insertObjectAtIndex: I believe there's some data you instantiate your UITableViewController with in the normal workflow of the app (when segueing to it) and when you try to present the UITableView controller in a clean state from a notification, that data is missing, hence the crash.
Check where you try to add an object to an array at a given index.
The problem is either in the class that receives the notification (I guess it's your AppDelegate class), either in the UITableViewController class, somewhere in the initialization.
Try
Home_tableView *home =[storyboard instantiateViewControllerWithIdentifier:#"home_view"];
[self.window setRootViewController:home]
[self.window makeKeyAndVisible];
Please try this:
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
Home_tableView*svc = [storyboard instantiateViewControllerWithIdentifier:#"home_view"];
// Configure the new view controller here.
[self presentViewController:svc animated:YES completion:nil];

Setting MFSideMenu after showing user content with UIPageViewController - iOS

I am using storyboard in app which first shows a First Time Usage Page with UIPageViewController.
When it is finished and user signs up, it will head to MFSideMenu.
I am holding MFSideMenu in another storyboard.
When user presses to sign up and it is successful the method below is fired.
-(IBAction)continueButtonPressed:(id)sender
{
AppDelegate *appDel = [UIApplication sharedApplication].delegate;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SideMenu" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)appDel.window.rootViewController;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"rightMenuViewController"];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setRightMenuViewController:rightSideMenuViewController];
[container setCenterViewController:navigationController];
[appDel.window makeKeyAndVisible];
}
But i am taking exception as
[UINavigationController setLeftMenuViewController:]: unrecognized selector sent to instance
What am i missing?
Could you please help me?
This is because you are choosing a navigation view as your root view Please set your sidemen container view as ur initial view in your storyboard. This will work for your View.
Please refer the image:

Parent and child view in Container V

I have a project in iOS and I am trying to modify the same to use in another project. The project is working fine but when I try and Embed the Side View Controller in a tab bar controller it is giving an error
**MFSideMenuDemoStoryboard[23760:c07] -[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590
2013-06-13 10:08:51.062 MFSideMenuDemoStoryboard[23760:c07] **** * * **Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590'**
i understand that there is something wrong in the Code in appDelegate .m but can't figure out
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"rightSideMenuViewController"];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setRightMenuViewController:rightSideMenuViewController];
[container setCenterViewController:navigationController];
return YES;
}
This is my storyboard
Your root view controller is not a MFSideMenuContainerViewController. It is a UITabBarController. If you want to use MFSideMenuContainerViewController you will want to check out the documentation: https://github.com/mikefrederick/MFSideMenu/
Here is a basic example:
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"rightSideMenuViewController"];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController: self.window.rootViewController
leftMenuViewController: leftSideMenuViewController
rightMenuViewController: rightSideMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
This will replace your existing root view controller with a MFSideMenuContainerViewController and place your old root view controller as the center view controller.
As you can see in the storyboard and in the exception, your root view controller is a tab bar controller, not a side menu controller.
UITabBarController has a property viewControllers which gives you access to the controllers inside the tabBarController. You want to get the controller at index 0. This means the viewController at the first tab.
Something like this should work:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
MFSideMenuContainerViewController *container = tabBarController.viewControllers[0];
// check that container is actually a container and not something else
NSParameterAssert([container isKindOfClass:[MFSideMenuContainerViewController class]]);

Resources