Im working on a project in Swift 3.0 and I have an authentication sign up procedure using Facebook. So basically I've done the FB integration part and once logged in to an account as a result I'm getting the user's email address, name,Id and the url of the profile pic as bellow since i have requested them (this is what I get in my console). The format that I receive as bellow.
Optional({
email = "[email protected]";
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";
};
};
})
My requirement is to assign these email, name, id field in to labels and print them out.
The code of my relevant view controller as bellow. What should I add ??
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
}
}
}
First specify the type of your result object to [String: Any]
and then get value from it.
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
}