ChuckKelly ChuckKelly - 6 months ago 24
Objective-C Question

How to get notifications on successful FBLogin when using a custom login button?

Facebook gives this little blurb:

enter image description here

Which is all well and great if your going to use their corny login button, but what about when ur using a custom one that calls a method like this:

-(void)loginWithFacebook{

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"email",@"user_about_me",@"user_hometown",@"user_education_history",@"user_friends",@"user_likes",@"user_likes",@"user_location",@"user_hometown",@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"FB_LOGIN_SUCCESS");
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);
}else{

NSLog(@"FB_ME_REQUEST_ERROR:%@",error);
}
}];
}
}
}];

}

Answer

UPD: You can create custom LoginButton and add target action for button like that:

- (void)createLoginButton {
   UIButton *loginButton = [[UIButton alloc]init];
   [loginButton setTitle:@"Login" forState:UIControlStateNormal];
   [loginButton addTarget:self action:@selector(loginButtonPressed) forControlEvents:UIControlEventTouchUpInside];
   //any customisation here
} 

Than you can use your login method but add some code for posting a UILocalNotification:

    -(void)loginButtonPressed {
       FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
        [login
         logInWithReadPermissions: @[@"email",@"user_about_me",@"user_hometown",@"user_education_history",@"user_friends",@"user_likes",@"user_likes",@"user_location",@"user_hometown",@"public_profile"]
         fromViewController:self
         handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                 NSLog(@"Process error");
             } else if (result.isCancelled) {
                 NSLog(@"Cancelled");
             } else {
                 NSLog(@"FB_LOGIN_SUCCESS");
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"FacebookButtonDidLoginWithSuccess" object:nil];
             }
         }];
    }

That, in class where you has all login logic, you should add this code to your viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(facebookLoginDidReceiveResult:) 
        name:@"FacebookButtonDidLoginWithSuccess"
        object:nil];

Also you should add next code to dealloc method:

[[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];

After use this result method:

- (void) facebookLoginDidReceiveResult:(NSNotification *) notification
{
    //implement here your login in case of successful login to Facebook
    //for example send your graph request
    if ([FBSDKAccessToken currentAccessToken]) {
                 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
                  startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                      if (!error) {
                          NSLog(@"fetched user:%@", result);
                      }else{

                          NSLog(@"FB_ME_REQUEST_ERROR:%@",error);

                      }
                  }];
}

I hope this will help you.

Comments