简体   繁体   中英

how to login with Facebook with preinstalled facebook app in iOS 9

(IBAction)loginWithfacebookClicked:(id)sender
{
    // When Clicked on Facebook Button

    if (FBSession.activeSession.state == FBSessionStateOpen
    || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        [FBSession.activeSession closeAndClearTokenInformation];

        // If the session state is not any of the two "open" states when the button is clicked
    } else {
        // Open a session showing the user the login UI
        [FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]
                                       allowLoginUI:YES
                                  completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];

         }];
    } 
}

Use this logic inside Facebook button click

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];    
//***Start : requesting for facebook login with valid permissions***
        [login logInWithReadPermissions: @[@"public_profile"]
                                       handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                                           if (error) {
                                               NSLog(@"Process error");

                                           }
                                           else if(result.isCancelled){
                                               NSLog(@"Cancelled");
                                           }
                                           else {

                                               //Successfully logged in
                                               NSLog(@"Logged in");

                                               //dictionary to represent the data to be fetched from facebook
                                               NSDictionary *params = @{  @"fields":@"name,gender,id,picture"};

                                               //Setting up the request parameters
                                               FBSDKGraphRequest *requestForUserProfileDetails = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:params HTTPMethod:@"GET"];

                                               //***Start : requesting for user profile details***
                                               [requestForUserProfileDetails startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection ,id result, NSError* error){

                                                   if(!error)
                                                   {
                                                       //set login type as facebook


                                                       //result contains the json response sent by facebook
                                                       _socialLoginType = @"facebook";
                                                       _name = [result valueForKey:@"name"];
                                                       _gender = [result valueForKey:@"gender"];
                                                       _socialLoginKey = [result valueForKey:@"id"];

                                                       _profileImage = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"]]]];

                                                   }

                                               }];
                                               //***End : requesting for user profile details***
                                           }

                                       }];
        //***End : requesting for facebook login with valid permissions***

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