iOS Facebook获得用户的电子邮件

时间:2014-07-22 14:56:26

标签: ios facebook email sdk

我尝试了超过2000件事来获取用户的电子邮件。我无法从Facebook SDK的图形API中获取它。它不包含电子邮件属性。我还试图手动将电子邮件属性添加到FB框架,没有任何反应。是否可以下载与iOS 7兼容的第一个FB SDK?它是否还有电子邮件属性,不是吗?或者有没有其他方法如何获得真正的电子邮件,我必须与他们合作。我不需要example@facebook.com。

感谢您的任何建议。

编辑

NSArray *permissions = @[@"email", @"public_profile"];
    [PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
        if (!user) {
            if (!error) {
                NSLog(@"The user cancelled the Facebook login.");
            } else {
                NSLog(@"An error occurred: %@", error.localizedDescription);
            }


            if ([delegate respondsToSelector:@selector(commsDidLogin:)]) {
                [delegate commsDidLogin:NO];
            }
        } else {
            if (user.isNew) {
                NSLog(@"User signed up and logged in through Facebook!");
            } else {
                NSLog(@"User logged in through Facebook!");
            }

            [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    PFUser *usere = [PFUser currentUser];
                    [usere setObject:[result objectForKey:@"first_name"] forKey:@"name"];
                    [usere setObject:[result objectForKey:@"last_name"] forKey:@"surname"];
                 //   [usere setObject:[result objectForKey:@"email"] forKey:@"mail"];
                    [usere saveEventually];
                     NSLog(@"user info: %@", result);
                        }
            }];

                }
                if ([delegate respondsToSelector:@selector(commsDidLogin:)]) {
                    [delegate commsDidLogin:YES];
                }
            }];
        }

5 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

我没有图API的代码,

但是使用新的facebook sdk版本3,我有代码。

-(void)openFbSession
{
    [[self appDelegate].session closeAndClearTokenInformation];

    NSArray *permissions =     [NSArray arrayWithObjects:@"email",@"user_location",@"user_birthday",@"user_hometown",nil];
    [self appDelegate].session = [[FBSession alloc] initWithPermissions:permissions];

    [[self appDelegate].session openWithCompletionHandler:^(FBSession *session,
                                                            FBSessionState status,
                                                            NSError *error) {
        if(!error)
        {
            NSLog(@"success");
            [self myFbInfo];
        }
        else
        {
            NSLog(@"failure");
        }

    }];
}

并且对于所有信息,myFbInfo方法是

-(void)myFbInfo
{
    [FBSession setActiveSession:[self appDelegate].session];

    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {
        if (error) {
            // Handle error
        }

        else {
            //NSString *userName = [FBuser name];
            //NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser id]];
            NSLog(@"Name : %@",[FBuser name]);
            NSLog(@"first name : %@",[FBuser first_name]);
            NSLog(@"Last name : %@",[FBuser last_name]);
            NSLog(@"ID : %@",[FBuser id]);
            NSLog(@"username : %@",[FBuser username]);
            NSLog(@"Email : %@",[FBuser objectForKey:@"email"]);

            NSLog(@"user all info : %@",FBuser);

              }
    }];

}

修改


在appdelegate.h中

@property (strong, nonatomic) FBSession *session;
在appdelegate.m

- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation
{
    //NSLog(@"FB or Linkedin clicked");
    return [self.session handleOpenURL:url];
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBSession.activeSession handleDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self.session close];
}

答案 2 :(得分:0)

您无法获取用户的电子邮件地址,您必须征得他们的同意。看看这个:
https://developers.facebook.com/docs/facebook-login/permissions/v2.0

Facebook Connect将允许在您的身份验证呼叫的scope=email字符串中传递get

答案 3 :(得分:0)

我无法使用上述示例。我使用对FBSDKGraphRequest的更复杂的调用解决了它。

在viewDidLoad中:

if(FBSDKAccessToken.currentAccessToken() != nil) {

  println("Logged in to FB")
  self.returnUserData()  //Specified here below

} else {
  print("Not logged in to FB")

  let loginView : FBSDKLoginButton = FBSDKLoginButton()
  loginView.center = self.view.center
  loginView.readPermissions = ["public_profile", "email", "user_friends"]
  loginView.delegate = self
  self.view.addSubview(loginView)
  }
}

上述读取权限对于以后在您从FB服务器请求时能够获取它是很重要的。

请记住通过包含所需的功能(此处未包含)来符合“FBSDKLoginButtonDelegate”协议。

为了能够获取电子邮件等,我使用了更复杂的graphRequest调用,并指定了accesstoken和参数(见下文)。

 let fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString   

    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(
    graphPath: "me", 
    parameters: ["fields":"email,name"], 
    tokenString: fbAccessToken, 
    version: nil, 
    HTTPMethod: "GET")

...并在同一函数中使用completionHandler执行:

graphRequest.startWithCompletionHandler({ (connection, result, error) -> () in result

  if ((error) != nil) {
    // Process error
    println("Error: \(error)")
  } else {
    println("fetched user: \(result)")
  }
}

工作得很漂亮!

另外..参数列在这里:

https://developers.facebook.com/docs/graph-api/reference/v2.2/user

答案 4 :(得分:0)

        if ([result.grantedPermissions containsObject:@"email"]) {
            // Do work
            NSLog(@"%@",[FBSDKAccessToken currentAccessToken]);

            if ([FBSDKAccessToken currentAccessToken]) {
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me" parameters:[NSMutableDictionary dictionaryWithObject:@"picture.type(large),id,email,name,gender" forKey:@"fields"] tokenString:result.token.tokenString version:nil HTTPMethod:@"GET"]
                 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                     if (!error) {

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

                     }
                 }];
            }

        }