如何在iOS 9中使用预装的Facebook应用程序登录Facebook

时间:2015-12-07 07:09:06

标签: ios facebook

(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];

         }];
    } 
}

1 个答案:

答案 0 :(得分:1)

在Facebook按钮内单击

使用此逻辑
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***