检索电子邮件 FacebookSDK 和 Swift
Retrieve email FacebookSDK and Swift
我在我的应用程序中成功实现了 facebook 登录,但我无法 query/retrieve 来自用户的电子邮件地址。
我有以下代码:
import UIKit
class testViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
// Or Show Logout Button
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self
self.returnUserData()
}
else
{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self
}
}
// Facebook Delegate Methods
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
}
self.returnUserData()
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("User Logged Out")
}
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
println("User Name is: \(userName)")
let userEmail : NSString = result.valueForKey("email") as! NSString
println("User Email is: \(userEmail)")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
并且 println 输出:
fetched user: {
id = 808101565906152;
name = "Marcelo Pontes Machado";
}
当我尝试显示电子邮件时,我得到了一个空值。
我认为代码是正确的,也许我错过了一些导入的SDK?
将您想要从 Graph 请求返回的字段放入参数中
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,picture.width(480).height(480)"]).startWithCompletionHandler({
因为图表 API 有时可能只有 return 最少量的信息,除非另有要求。
在Swift
if((FBSDKAccessToken.current()) != nil)
{
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler:
{ (connection, result, error) -> Void in
if (error == nil)
{
//everything works print the user data
// print(result!)
if let data = result as? NSDictionary
{
let firstName = data.object(forKey: "first_name") as? String
let lastName = data.object(forKey: "last_name") as? String
if let email = data.object(forKey: "email") as? String
{
print(email)
}
else
{
// If user have signup with mobile number you are not able to get their email address
print("We are unable to access Facebook account details, please use other sign in methods.")
}
}
}
})
}
在 Objective C 中连接此代码
// START_LOADING;
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, email,first_name,last_name"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
if ([result valueForKey:@"email"] != nil)
{
NSLog([result valueForKey:@"email"]);
}
else
{
// If user have signup with mobile number you are not able to get their email address
NSLog(@"We are unable to access Facebook account details, please use other sign in methods.");
// STOP_LOADING;
}
}
}];
我在我的应用程序中成功实现了 facebook 登录,但我无法 query/retrieve 来自用户的电子邮件地址。
我有以下代码:
import UIKit
class testViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
// Or Show Logout Button
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self
self.returnUserData()
}
else
{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self
}
}
// Facebook Delegate Methods
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
}
self.returnUserData()
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("User Logged Out")
}
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
println("User Name is: \(userName)")
let userEmail : NSString = result.valueForKey("email") as! NSString
println("User Email is: \(userEmail)")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
并且 println 输出:
fetched user: {
id = 808101565906152;
name = "Marcelo Pontes Machado";
}
当我尝试显示电子邮件时,我得到了一个空值。
我认为代码是正确的,也许我错过了一些导入的SDK?
将您想要从 Graph 请求返回的字段放入参数中
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,picture.width(480).height(480)"]).startWithCompletionHandler({
因为图表 API 有时可能只有 return 最少量的信息,除非另有要求。
在Swift
if((FBSDKAccessToken.current()) != nil) { FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in if (error == nil) { //everything works print the user data // print(result!) if let data = result as? NSDictionary { let firstName = data.object(forKey: "first_name") as? String let lastName = data.object(forKey: "last_name") as? String if let email = data.object(forKey: "email") as? String { print(email) } else { // If user have signup with mobile number you are not able to get their email address print("We are unable to access Facebook account details, please use other sign in methods.") } } } }) }
在 Objective C 中连接此代码
// START_LOADING; [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture, email,first_name,last_name"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { if ([result valueForKey:@"email"] != nil) { NSLog([result valueForKey:@"email"]); } else { // If user have signup with mobile number you are not able to get their email address NSLog(@"We are unable to access Facebook account details, please use other sign in methods."); // STOP_LOADING; } } }];