获取电子邮件并命名Facebook SDK v4.4.0 Swift [英] Get email and name Facebook SDK v4.4.0 Swift
问题描述
TL; TR:如何获取使用Facebook SDK 4.4在我的应用程序上登录的用户的电子邮件和名称
TL;TR: How do I get the email and name of a user that is logged in on my app using the facebook SDK 4.4
到目前为止,我已经成功登录,现在可以从应用程序中的任何位置获取当前访问令牌.
So far I have managed to get login working, now I can get the current access token from anywhere in the app.
我如何配置我的登录视图控制器和Facebook登录按钮:
How I have my login view controller and facebook login button configured:
class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var loginButton: FBSDKLoginButton!
override func viewDidLoad() {
super.viewDidLoad()
if(FBSDKAccessToken.currentAccessToken() == nil)
{
print("not logged in")
}
else{
print("logged in already")
}
loginButton.readPermissions = ["public_profile","email"]
loginButton.delegate = self
}
//MARK -FB login
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
//logged in
if(error == nil)
{
print("login complete")
print(result.grantedPermissions)
}
else{
print(error.localizedDescription)
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
//logout
print("logout")
}
现在在我的主视图中,我可以像这样获得访问令牌:
Now on my main view I can get the access token like so:
let accessToken = FBSDKAccessToken.currentAccessToken()
if(accessToken != nil) //should be != nil
{
print(accessToken.tokenString)
}
如何从登录的用户那里获得名称和电子邮件,使用较旧的SDK或使用Objective-C,我会看到很多问题和答案.
How do I get the name and email from the user that is logged in, I see many question and answers using eather an older SDK or using Objective-C.
推荐答案
我已经在android中使用了fields
,所以我也想在iOS中也尝试使用它,并且可以使用.
I've used fields
in android, so I figured to try it in iOS as well, and it works.
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil) {
print("result \(result)")
} else {
print("error \(error)")
}
}
)
结果将打印:
result {
email = "email@example.com";
id = 123456789;
name = "Your Name";
}
发现这些字段等于User
端点,请参见此链接,您可以在其中查看可以获取的所有字段.
Found that these fields are equal to the User
endpoint, see this link where you can see all the fields that you can get.
针对Swift 4及更高版本进行更新
Update for Swift 4 and above
let r = FBSDKGraphRequest(graphPath: "me",
parameters: ["fields": "email,name"],
tokenString: FBSDKAccessToken.current()?.tokenString,
version: nil,
httpMethod: "GET")
r?.start(completionHandler: { test, result, error in
if error == nil {
print(result)
}
})
使用FBSDKLoginKit 6.5.0更新Swift 5
Update for Swift 5 with FBSDKLoginKit 6.5.0
guard let accessToken = FBSDKLoginKit.AccessToken.current else { return }
let graphRequest = FBSDKLoginKit.GraphRequest(graphPath: "me",
parameters: ["fields": "email, name"],
tokenString: accessToken.tokenString,
version: nil,
httpMethod: .get)
graphRequest.start { (connection, result, error) -> Void in
if error == nil {
print("result \(result)")
}
else {
print("error \(error)")
}
}
这篇关于获取电子邮件并命名Facebook SDK v4.4.0 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!