简体   繁体   中英

How to logout facebook in my app using Facebook SDK

I have integrated Facebook login in my app and therefore user can login with both my app account and also Facebook and do corresponding actions.For Facebook integration I have added Facebook SDK .Now when Logout button is clicked in my app it has to clear all the credentials of Facebook Account.But when I again click on button I'm directly login to my account without going to Facebook Login page.

How can I logout of Facebook?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    [self toggleHiddenState:YES];

    self.fbLoginStates.text = @"";
    self.loginButton.delegate = self;
    self.loginButton.readPermissions = @[@"public_profile", @"email"];
}

-(void)toggleHiddenState:(BOOL)shouldHide    
{
    self.lblUsername.hidden = shouldHide;
    self.lblEmail.hidden = shouldHide;
    self.profilePicture.hidden = shouldHide;
}

#pragma mark - FBLoginView Delegate method implementation

-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{        
    self.fbLoginStates.text = @"You are logged in.";

    [self toggleHiddenState:NO];
}

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{        
    NSLog(@"%@", user);

    self.profilePicture.profileID = user.id;
    self.lblUsername.text = user.name;
    self.lblEmail.text = [user objectForKey:@"email"];
}

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
    self.fbLoginStates.text = @"You are logged out";

    [self toggleHiddenState:YES];
}

-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
    NSLog(@"%@", [error localizedDescription]);
}

In AppDelegate .m

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

In AppDelegate .m

return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];

In Login Screen .m:-

- (void)FacebookLogin:(id)sender {
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getFacebookData) name:@"getFacebookData" object:nil];
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"]
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
         } else {
             NSLog(@"Logged in");
             [MBProgressHUD showHUDAddedTo:self.view animated:YES];
              FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                           initWithGraphPath:@"/me"
                                           parameters:@{ @"fields": @" email",}
                                           HTTPMethod:@"GET"];
             [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                 // Insert your code here
                 NSLog(@"%@",result);              
              [MBProgressHUD hideHUDForView:self.view animated:YES];
              } failedBlock:^{
              [MBProgressHUD hideHUDForView:self.view animated:YES];
                  UIAlertController * alert=   [UIAlertController alertControllerWithTitle:APP_NAME message:@"Could not connect to the server." preferredStyle:UIAlertControllerStyleAlert];
                  UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                                             {
                                                 //Handel your yes please button action here
                                                 [alert dismissViewControllerAnimated:YES completion:nil];

                                             }];
                  [alert addAction:okButton];
                  [self presentViewController:alert animated:YES completion:nil];
              }];

             }];

         }
     }];
}

In Logout screen .m :-

 -(IBAction)Logout:(id)sender
{
       [[FBSDKLoginManager new] logOut];    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM