将 facebook 集成的结果设置为标签
set the results of facebook integration to labels
我在 Swift 3.0 中从事一个项目,我有一个使用 Facebook 的身份验证注册程序。所以基本上我已经完成了 FB 集成部分,一旦登录到一个帐户,结果我得到了用户的电子邮件地址、姓名、Id 和个人资料图片的 url,如下所示,因为我已经请求了他们(这是我在控制台中得到的)。我收到的格式如下。
Optional({
email = "xxxxx@yahoo.com";
id = xxxxxxxxxx;
name = "xxxxxx xxxx";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1-1/c2.0.50.50/p50x50/10441341_10201209786847369_5426097469926927988_n.jpg?oh=1b4d8709be17338bc7e877d411b96eee&oe=58CE5B13";
};
};
})
我的要求是将这些电子邮件、姓名、ID 字段分配给标签并打印出来。
我的相关视图控制器的代码如下。我应该添加什么??
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController,FBSDKLoginButtonDelegate{
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = FBSDKLoginButton()
view.addSubview(loginButton)
//frame's are obselete, please use constraints instead because its 2016 after all
loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
loginButton.delegate = self
loginButton.readPermissions = ["email", "public_profile"]
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Did log out of facebook")
}
@IBAction func loginButtonPressed(_ sender: AnyObject) {
FBSDKLoginManager().logIn(withReadPermissions: ["email"], from: self) { (result, err) in
if err != nil {
print("Custom FB Login failed:", err)
return
}
print("FB login Sucess")
print(result?.token.tokenString)
self.showEmailAddress()
}
}
func showEmailAddress() {
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email,picture"]).start { (connection, result, err) in
if err != nil {
print("Failed to start graph request:", err)
return
}
print(result)
// self.displayLabel.text = result["email"]as? NSMutableArray
}
}
}
首先将结果对象的类型指定为 [String: Any]
,然后从中获取值。
if let dic = result as? [String: Any] {
self.displayLabel.text = dic["email"] as? String
self.displayNameLabel.text = dic["name"] as? String
self.displayIdLabel.text = dic["id"] as? String
}
我在 Swift 3.0 中从事一个项目,我有一个使用 Facebook 的身份验证注册程序。所以基本上我已经完成了 FB 集成部分,一旦登录到一个帐户,结果我得到了用户的电子邮件地址、姓名、Id 和个人资料图片的 url,如下所示,因为我已经请求了他们(这是我在控制台中得到的)。我收到的格式如下。
Optional({
email = "xxxxx@yahoo.com";
id = xxxxxxxxxx;
name = "xxxxxx xxxx";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1-1/c2.0.50.50/p50x50/10441341_10201209786847369_5426097469926927988_n.jpg?oh=1b4d8709be17338bc7e877d411b96eee&oe=58CE5B13";
};
};
})
我的要求是将这些电子邮件、姓名、ID 字段分配给标签并打印出来。
我的相关视图控制器的代码如下。我应该添加什么??
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController,FBSDKLoginButtonDelegate{
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = FBSDKLoginButton()
view.addSubview(loginButton)
//frame's are obselete, please use constraints instead because its 2016 after all
loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
loginButton.delegate = self
loginButton.readPermissions = ["email", "public_profile"]
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Did log out of facebook")
}
@IBAction func loginButtonPressed(_ sender: AnyObject) {
FBSDKLoginManager().logIn(withReadPermissions: ["email"], from: self) { (result, err) in
if err != nil {
print("Custom FB Login failed:", err)
return
}
print("FB login Sucess")
print(result?.token.tokenString)
self.showEmailAddress()
}
}
func showEmailAddress() {
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email,picture"]).start { (connection, result, err) in
if err != nil {
print("Failed to start graph request:", err)
return
}
print(result)
// self.displayLabel.text = result["email"]as? NSMutableArray
}
}
}
首先将结果对象的类型指定为 [String: Any]
,然后从中获取值。
if let dic = result as? [String: Any] {
self.displayLabel.text = dic["email"] as? String
self.displayNameLabel.text = dic["name"] as? String
self.displayIdLabel.text = dic["id"] as? String
}