'NSInvalidArgumentException' on UILocalNotifcation Load - ios

When loading my application from a local notification I am trying to read its payload. To do so I have the following code:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Loading Stuff
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
[(UITabBarController *)self.window.rootViewController setSelectedIndex:1];
UINavigationController *nav = [[(UITabBarController *)self.window.rootViewController viewControllers] objectAtIndex:1];
IMTRewardsViewController *rvc = [storyboard instantiateViewControllerWithIdentifier:#"rewardsView"];
[rvc loadPushNotification:localNotif];
[nav pushViewController:rvc animated:NO];
}
return YES;
}
IMTRewardsController.h:
-(NSDictionary *)loadPushNotification:(UILocalNotification *)notification;
IMTRewardsController.m:
- (NSDictionary *)loadPushNotification:(UILocalNotification *)notification
{
NSLog(#"%#",notification.userInfo);
return notification.userInfo;
}
When I load my application from a local notification I receive the following error:
<Error>: -[UIViewController loadPushNotification]: unrecognized selector sent to instance 0x14e70b30
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController loadPushNotification]: unrecognized selector sent to instance 0x14e70b30'
Any idea as to how to fix this issue and keep it from cropping up in the future?

There error indicates the view controller you've pulled back is only a UIViewController, and not a IMTRewardsViewController like you're expecting. Are you sure you set the custom class property to that type in the storyboard?

You might need to cast it first. The storyboard returns an UIViewController
IMTRewardsViewController *rvc = (IMTRewardsViewController *)[storyboard instantiateViewControllerWithIdentifier:#"rewardsView"];

Related

3D Touch for Shortcuts Crashes

My app is a single-view NavigationController as a root view controller style app. In it, I have a few different shortcut items for using 3D Touch. I have them all set up in the Info.plist fine (I've done this before with a Tab Bar app and it worked fine), but it crashes every time an shortcut action is pressed. Here is my the code used in AppDelegate in Obj-C.
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
UINavigationController *nav = (UINavigationController *) self.theMainView.view;
NSLog(#"%#", shortcutItem.type);
if ([shortcutItem.type isEqualToString:#"com.316apps.Fritch.inviteFriends"]) {
ImagePicker *vimeo= [[ImagePicker alloc] init];
[nav pushViewController:vimeo animated:YES];
}
if ([shortcutItem.type isEqualToString:#"com.316apps.Fritch.viewAlerts"]) {
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
if ([shortcutItem.type isEqualToString:#"com.316apps.Fritch.viewDirectory"]) {
DirectoryViewController *dvController8 = [[DirectoryViewController alloc] init];
[nav pushViewController:dvController8 animated:YES];
}
}
Crash Log:
com.316apps.Fritch.viewDirectory
2017-01-19 22:44:23.305906 Fritch[3956:925348] -[UILayoutContainerView pushViewController:animated:]: unrecognized selector sent to instance 0x101837530
2017-01-19 22:44:23.306768 Fritch[3956:925348] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILayoutContainerView pushViewController:animated:]: unrecognized selector sent to instance
UINavigationController *nav = (UINavigationController *) self.theMainView.view;
The reason of crashing is you are getting the view, not ViewController. So self.theMainView.view cannot convert to UINavigationController. If your self.theMainView is correct, fix that crash by using:
UINavigationController *nav = (UINavigationController *) self.theMainView;

Navigating from Appdelegate when push message received

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

Pass data in UISplitViewController

I want to pass data from Master view controller to Detail view controller according to the picture below. I am using protocols and Delegates to pass data. There is no problem to pass data from A to B. It happens when I try to pass data from A to C.
But the app is returning the error:
014-07-05 01:03:00.043 PassingDataBack[2216:70b] -[UIViewController
didSelectData:]: unrecognized selector sent to instance 0x8e3d2d0
2014-07-05 01:03:00.047 PassingDataBack[2216:70b] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[UIViewController didSelectData:]: unrecognized selector sent to
instance 0x8e3d2d0'
I believe the problem is in the AppDelegate file that i wrote:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
splitViewController.delegate = [splitViewController.viewControllers lastObject];
DetailViewController *detailViewController = (DetailViewController *)[[splitViewController.viewControllers objectAtIndex:1] topViewController];
MasterViewController *masterViewController = (MasterViewController *)[[splitViewController.viewControllers objectAtIndex:0] topViewController];
masterViewController.delegate = detailViewController;
// Override point for customization after application launch.
return YES;
}
Could you help to find the solution?

performSegueWithIdentifier NSInvalidArgumentException after call from AppDelegate

Hey together,
I am calling a void with some parameters from the AppDelegate on my main view.
This is done if a push notification is received:
MainViewController *mainView = [[MainViewController alloc] init];
[mainView showPushView:pushDataObject];
The called void # the MainView doing some data operating stuff and after that it should load the pushView:
- (void)showPushView: (PFObject *)pushDataObject {
NSLog(#"Push Data object transfered %#", pushDataObject);
pushItem = pushDataObject;
//All working fine to this point
[self performSegueWithIdentifier:#"showPushObject" sender:self];
}
Now the problem is that the app is crashing at [self performSegueWithIdentifier:#"showPushObject" sender:self]; with this Error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Receiver (<MainViewController: 0x145b80e0>) has no segue with identifier 'showPushObject''
*** First throw call stack:
(0x2e51fe83 0x3887c6c7 0x30f656d9 0xbeb11 0xb2a23 0x1745f7 0x38d610c3 0x38d610af 0x38d639a9 0x2e4ea5b1 0x2e4e8e7d 0x2e453471 0x2e453253 0x3318d2eb 0x30d08845 0xafecd 0x38d75ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
I think that there is a problem because I call the void from the AppDelegate, am I right?
Those anyone know a fix for that problem?
Thanks a lot!
Best regards from Germany :)
P.S. If I call [self performSegueWithIdentifier:#"showPushObject" sender:self]; with a button or something on the MainViewController all working fine... :/
In order for the segue to work, you need to have the storyboard loaded in the mainView when you start it that way. Try instead something like this:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *firstViewController = [storyboard instantiateViewControllerWithIdentifier:#"kYourMainViewControllerIdentifier"];
self.window.rootViewController = firstViewController;
[self.window makeKeyAndVisible];
Remember to give an identifier to your root view controller and change it in this piece of code.
Your problem is this line:
MainViewController *mainView = [[MainViewController alloc] init];
because it means that the mainView instance doesn't have a storyboard (so it can't have any segues).
When you run it from a button the controller instance must have been created from a storyboard.
So, to fix, load the storyboard and instantiate mainView from it. Then the segue will work.
UIStoryboard *storyboard4Inch = [UIStoryboard storyboardWithName:#"Storyboard4Inch" bundle:nil];
UIViewController *mainViewController = [storyboard4Inch instantiateViewControllerWithIdentifier:#"MainViewController"];
[mainViewController showPushView:pushDataObject];

IOS open in works on simulator crashes on device with UIPopoverController error

In storyboard I have a view controller that displays pdf files if other apps try to "open in" with my app.
In simulator IOS 5.1 or IOS 6 + , it works perfectly but on device I get SIGABRT on main thread with this error:
[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
Bizarre thing I dont have any UIPopoverController in related viewcontrollers.
In find I search "UIPopoverController" and none of the related controllers (offlinereader,leftSideMenuViewController,navigationController) has a popovercontroller
#pragma OpeIn
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
// Make sure url indicates a file (as opposed to, e.g., http://)
if (url != nil && [url isFileURL]) {
NSLog(#"Url in app delegate= %#",url);
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
OfflineReaderViewController *registerVC = [sb instantiateViewControllerWithIdentifier:#"OfflineReaderViewController"];
// Override point for customization after application launch.
registerVC.filePath=url;
self.viewController = registerVC;
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:self.viewController animated:YES completion:NULL];
// Tell our OfflineReaderViewController to process the URL
[self.viewController handleDocumentOpenURL:url];
}
// Indicate that we have successfully opened the URL
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UITabBarController *navigationController = (UITabBarController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
[MFSideMenu menuWithNavigationController:navigationController
leftSideMenuController:leftSideMenuViewController
rightSideMenuController:nil];
return YES;
}
offlinereader is : UIViewController<UIDocumentInteractionControllerDelegate>
Whats going on?
How can I solve or find the exact problem?
EDIT::::
Full error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
*** First throw call stack:
(0x317932a3 0x3962997f 0x317931c5 0x33986d5f 0x338d7285 0x338d7b35 0xe49cf 0xe51a5 0x335ba595 0x33603fd7 0x33603f45 0x336884ef 0x33686fe7 0x51bb3 0x3376bb33 0x33744881 0x33743f6b 0x3359bd59 0x3359b6cd 0x3359b11b 0x3528f5a3 0x31768683 0x31767ee9 0x31766cb7 0x316d9ebd 0x316d9d49 0x3528e2eb 0x335ef301 0x50c89 0x50c10)
libc++abi.dylib: terminate called throwing an exception
SIGABRT errors are helpful follow this tutorial and find your exact line with problem.
http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
When you find the problem , there must be definetly related some popover code in your breakpoint line
If so add this
if (self.view.window != nil) {
//your popover code
}
Above code just basically checks if there is a window is present, duh run the code.

Resources