获取用户信息后,FBSDKLoginManager保持Facebook登录

时间:2016-11-19 18:19:59

标签: objective-c xcode facebook-sdk-4.x

我使用此获取用户名和电子邮件(见下文)。

问题是,登录webview关闭,调用initWithGraphPath:@" me"已完成异步,因此用户可以在响应此调用时看到空字段。

自从调用initWithGraphPath完成后,有没有办法让facebook登录webview保持打开状态?

谢谢!

-(void)loginButtonClicked_Facebook{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile", @"email"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
         } else {
             if ([result.grantedPermissions containsObject:@"email"]){
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
             }
         }
     }];
}

-(void)fetchUserInfo{
    if ([FBSDKAccessToken currentAccessToken]){
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error){
                 NSString *userName = [result valueForKey:@"name"];
                 NSString *email =  [result valueForKey:@"email"];
                 self.tfName.text=userName;
                 self.tfEmail.text=email;
                 self.tfEmailConfirmation.text=email;
                 NSLog(@"resultis:%@",result);
             }else{
                 NSLog(@"Error %@",error);
             }
         }];
    }
}

1 个答案:

答案 0 :(得分:0)

//YOURCONTROLLER.M

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


NSString *getFbid;
NSString *getFbFirstName,*getFBlastName, *getFbemail,*getfbBirthday,*getfbGender,*getFBpHone,*getFBlocation,*getFBcountry;





- (void)  loginButton:  (FBSDKLoginButton *)loginButton
didCompleteWithResult:  (FBSDKLoginManagerLoginResult *)result
            error:  (NSError *)error{

NSLog(@"facebook login button test");


}
- (void) loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{
NSLog(@"facebook logout button test");
}




-(void)loginButtonClicked
{



NSUserDefaults *defFacebookData = [NSUserDefaults standardUserDefaults];


FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
 logInWithReadPermissions: @[@"public_profile",  @"user_friends", @"email"]
 fromViewController:self
 handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
     if (error) {
         DLog(@"Process error======%@",error.description);
         indicators.hidden=YES;
         [indicators stopAnimating];
     } else if (result.isCancelled) {
         DLog(@"Cancelled");
         indicators.hidden=YES;
         [indicators stopAnimating];
     } else {

         if ([FBSDKAccessToken currentAccessToken]) {



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

                      //  NSLog(@"fetched user:%@", result);

                      // [self fetchingFacebookFriends];

                      [defFacebookData setObject:[result objectForKey:@"email"] forKey:@"fbEmail"];

                      [defFacebookData setObject:[result objectForKey:@"id"] forKey:@"fbID"];


                      //PASS ID
                      getFbid  = [result objectForKey:@"id"];
    NSLog(@"getFbid========>%@",getFbid);

                      //PASS FIRST NAME
                      getFbFirstName=[result objectForKey:@"first_name"];

    NSLog(@"first======>%@",getFbFirstName);

                      //PASS LAST NAME
                      getFBlastName=[result objectForKey:@"last_name"];
    NSLog(@"first======>%@",getFBlastName);

                      //PASS EMAIL
                      getFbemail=[result objectForKey:@"email"];
    NSLog(@"first======>%@",getFbemail);

                      //PASS PHONE
                      getfbGender=[result objectForKey:@"gender"];
    NSLog(@"first======>%@",getfbGender);


                      [defFacebookData setObject:[result objectForKey:@"name"] forKey:@"fbName"];

                      // Image
                      FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                                    initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
                                                    parameters:nil
                                                    HTTPMethod:@"GET"];
                      [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                            id fbImageResult,
                                                            NSError *error) {

NSString *strURL = [NSString stringWithFormat:@"%@",[[fbImageResult objectForKey:@"data"] objectForKey:@"url"]];

    NSLog(@"strURL=====>%@",strURL);

[defFacebookData setObject:strURL forKey:@"fbImage"];

                          [defFacebookData synchronize];


                          NSDictionary *fbdict=[NSDictionary dictionaryWithObjectsAndKeys:getFbid,@"id",getFbFirstName,@"first_name",getFBlastName,@"last_name",getFbemail,@"email",getfbGender,@"gender",strURL,@"fbImage", nil];

                          NSLog(@"done=========>%@",fbdict);

     UIStoryboard*storyboard=[AppDelegate storyBoardType];

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];


    BOOL fblogin =[defaults boolForKey:@"KeyEditProfile"];

        if (fblogin)
            {

                UIStoryboard*Storyboard=[AppDelegate storyBoardType];
                DashboardVC* tabbarController = (DashboardVC*)[Storyboard instantiateViewControllerWithIdentifier:@"DashboardVCId"];

// tabbarController.dictFacebookdict = fbdict; // tabbarController.strFBlogin = @&#34; fbAllDataValue&#34 ;;

                indicators.hidden=YES;
                [indicators stopAnimating];

                [self.navigationController pushViewController:tabbarController animated:YES];

        }

    else
    {
    EditFBVC *cpvc=(EditFBVC*)[storyboard instantiateViewControllerWithIdentifier:@"EditFBVCId"];
    NSLog(@"get fb id  ===%@",getFbid);

// cpvc.checkBtnclick = @&#34; 1&#34 ;;         cpvc.dictFacebookdict = fbdict;

    cpvc.strFBlogin =@"fbAllDataValue";

        indicators.hidden=YES;
        [indicators stopAnimating];

    [self.navigationController pushViewController:cpvc animated:YES];

    }                  //  [self facebookdataOnServer];
                      }];
                  }
                  else{
                      //[SVProgressHUD dismiss];
                      DLog(@"error is %@", error.description);
                  }
              }];
         }
     }
 }];

}