dany
dany

Reputation: 100

how to manage session in facebook integration in ios

i will succesfull login using facebook but i have problem because when i run app than everytime open facebook login screen but i want homescreen if user can logout than login screen open.so how to manage session.

here this is my code

- (IBAction)gb_login:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    login.loginBehavior = FBSDKLoginBehaviorBrowser;
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {


                 NSLog(@"result is:%@",result);

                 [self fetchUserInfo];

             }
         }
     }];

}
-(void)fetchUserInfo {

    if ([FBSDKAccessToken currentAccessToken]) {

        NSLog(@"Token is available");

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {


                 NSLog(@"Fetched User Information:%@", result);

                 NSLog(@"imagepath=%@",[[[result objectForKey:@"picture"]objectForKey:@"data"] objectForKey:@"url"]);
                 NSString *facebook=@"facebook";
                 [[NSUserDefaults standardUserDefaults] setObject:facebook forKey:@"logintype"];
                 [[NSUserDefaults standardUserDefaults] setObject:result forKey:@"facebooklogin"];
                 [[NSUserDefaults standardUserDefaults] synchronize];

             }
             else {
                 NSLog(@"Error %@",error);
             }
         }];

    } else {
       [[FBSDKLoginManager new] logOut];
        NSLog(@"User is not Logged in");
    }
}

Upvotes: 0

Views: 60

Answers (1)

Nitin Gohel
Nitin Gohel

Reputation: 49710

First you need to check do you have AccessToken In login screen ViewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];

      if ([FBSDKAccessToken currentAccessToken]) {

        [self getFacebookData];

    }
    else
    {
     // here use login code bcz you dont have accessToken
    }
}

And here you get the userInfo and save as per your need and push to next screen:

- (void)getFacebookData{
    if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, bio ,location , friends ,hometown , friendlists , gender"}]

         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);

               // here you need to code for push a next screen
             }

         }];
    }
}

Upvotes: 1

Related Questions