Think of a scenario here:

You have a class LoginViewController, while Facebook login api call is keep on AppDelegate.m, and you want to perform some action after success login via Facebook.

In AppDelegate.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
// call the method and pass the accessToken to it
[[NSNotificationCenter defaultCenter] postNotificationName:@"PerformFacebookLogin" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:session.accessTokenData.accessToken, @"fb_token", nil]];

break;
}
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Login failed or want to end the session
[FBSession.activeSession closeAndClearTokenInformation];

break;
default:
break;
}

if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Facebook login failed"
message:error.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}

- (void)openSession
{
NSArray *permissions = [NSArray arrayWithObjects:
@"email",
@"user_birthday",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}

In LoginViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)viewDidLoad
{
...

// listen to notification, then call facebookLogin method
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(facebookLogin:) name:@"PerformFacebookLogin" object:nil];
}
...
- (void)facebookLogin:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSString *token = [userInfo valueForKey:@"fb_token"];

// do what you want after success login via Facebook
}

References: