user1416492
user1416492

Reputation: 95

"Unrecognized selector sent to instance" error while trying to get AppDelegate

I follow Facebook's instruction (http://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/) and try to implement a facebook login button. However, I get the "Unrecognized selector sent to instance" error while trying to call the application delegate method.

The method:

- (IBAction)pressFBLogin:(id)sender {
    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate openSessionWithAllowLoginUI:YES];
}

From the error, I think it is memory pointer error pointing to a wrong instance. I set the break point at:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

It did reach this statement, so nothing wrong with action bound. The error shows up when it tries to run the next statement:

[appDelegate openSessionWithAllowLoginUI:YES];

There is one thing I found from the watch window that seems strange when I do the debug is the appDelegate variable. When break point is at

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

Variable is shown as:

appDelegate = (MyViewController *)0x003b9a10
> UIViewController(UIViewController)
> delegate = (objc_object *) 0x0033edb0

After the variable assignment, and break point is at

[appDelegate openSessionWithAllowLoginUI:YES];

Variable is shown as:

appDelegate = (MyViewController *)0x00316480
> UIViewController(UIViewController)
> delegate = (objc_object *) 0x00000000

In neither stage, the appDelegate seems to be an object of MyAppDelegate.

Upvotes: 0

Views: 4441

Answers (2)

Prince Kumar
Prince Kumar

Reputation: 981

I m facing this same crash, I have solved this by adding

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

In my APP Delegate, this solved my issue. I m using Xcode 12.3, This crash happens when I m trying to signup by using firebase auth

Upvotes: 0

user1416492
user1416492

Reputation: 95

I resolved my issue. It ends up to be nothing wrong with appDelegate variable. The problem is very simple: a little typo between my header and my implementation file of the openSessionWithAllowLoginUI function. Such an issue usually will be detected at compile time as error, I am surprise there is no build error on such a issue, instead just a warning of incomplete implementation...

Upvotes: 2

Related Questions