FacebookSDK(4.1.x) Custom Login UI Button - Swift(1.2) - ios

Following this tutorial, I have managed to make a Facebook Login Button working. However, it is assigning the button image automatically from the SDK and it is not customisable (as it is not getting created on Storyboard), so I am unable to use my own button image or text instead.
I believe this part of the code (in ViewDidLoad) is assigning the button:
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email"]
loginView.delegate = self
What I need to do is creating a #IBOutlet Button on Storyboard and customise it from there. How can I do that?

Login with custom button and access token.
Get user info in facebook sdk 4.x
Swift
#IBAction func btnFBLoginPressed(sender: AnyObject) {
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
}

Updated answer using the last Facebook SDK (01/05/2016) and Swift 2.1
Create a UIButton with Interface Builder or by code and link the action of that button with this:
#IBAction func loginFacebookAction(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
The happy case of the previous code triggers the function self.getFBUserData() so you have to implement that function in the same file
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}

I did it successfully in xcode 7.3
#IBAction func fbLoginBtnAction(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: {(result, error) -> Void in
if error == nil {
print("Logged in through facebook" )
self.getFBUserData()
}
else {
print("Facebook Login Error----\n",error)
}
}
)
}
func getFBUserData () {
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
print(result)
}
})
}
}

Reusable class (Swift 4).
Usage: FacebookSignIn.shared.signIn(from: yourVC, completion: yourCompletion)
class FacebookSignIn {
enum Error: Swift.Error {
case unableToInitializeGraphRequest
case unexpectedGraphResponse
case permissionsIsNotGranted
case unexpectedLoginResponse
case canceled
}
struct Permissions {
static let email = "email"
static let profile = "public_profile"
static func isValidPermissions(_ permissions: Set<AnyHashable>) -> Bool {
return permissions.contains(email) && permissions.contains(profile)
}
static var permissions: [String] {
return [email, profile]
}
}
public static let shared = FacebookSignIn()
private init() {
}
}
extension FacebookSignIn {
func signIn(from: UIViewController, completion: Result<SignInResponse>.Completion?) {
let manager = FBSDKLoginManager()
manager.loginBehavior = .native
if !isValidToken {
manager.logOut()
}
if let token = FBSDKAccessToken.current() {
let interval = token.expirationDate.timeIntervalSince(Date())
if interval > 300 { // At least 5 min token will be valid
performLogin {
switch $0 {
case .failure(let error):
completion?(.failure(error))
case .success(let info):
completion?(.success(SignInResponse(accessToken: token.tokenString, userInfo: info)))
}
}
} else {
FBSDKAccessToken.refreshCurrentAccessToken { [weak self] _, _, error in
if let error = error {
manager.logOut()
completion?(.failure(error))
} else {
let token = FBSDKAccessToken.current()?.tokenString ?? "" // Should be always valid value at this point.
self?.performLogin {
switch $0 {
case .failure(let error):
completion?(.failure(error))
case .success(let info):
completion?(.success(SignInResponse(accessToken: token, userInfo: info)))
}
}
}
}
}
} else {
manager.logIn(withReadPermissions: Permissions.permissions, from: from) { [weak self] result, error in
if let error = error {
manager.logOut()
completion?(.failure(error))
return
}
guard let result = result else {
manager.logOut()
completion?(.failure(Error.unexpectedLoginResponse))
return
}
let permissions = result.grantedPermissions ?? Set<AnyHashable>()
let token = result.token?.tokenString ?? "" // Should be always valid value at this point.
if result.isCancelled {
manager.logOut()
completion?(.failure(Error.canceled))
} else if Permissions.isValidPermissions(permissions) {
self?.performLogin {
switch $0 {
case .failure(let error):
completion?(.failure(error))
case .success(let info):
completion?(.success(SignInResponse(accessToken: token, userInfo: info)))
}
}
} else {
manager.logOut()
completion?(.failure(Error.permissionsIsNotGranted))
}
}
}
}
private var isValidToken: Bool {
guard let token = FBSDKAccessToken.current() else {
return false
}
return Permissions.isValidPermissions(token.permissions ?? Set<AnyHashable>())
}
private func makeGraphRequest() -> FBSDKGraphRequest? {
guard FBSDKAccessToken.current().tokenString != nil else {
return nil
}
// You might not get email: https://developers.facebook.com/docs/facebook-login/permissions/v2.4
// Note, even if you request the email permission it is not guaranteed you will get an email address. For example,
// if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty.
let fields = "email,id,first_name,last_name,gender"
return FBSDKGraphRequest(graphPath: "me", parameters: ["fields": fields])
}
private func performLogin(completion: Result<[String: String]>.Completion?) {
let manager = FBSDKLoginManager()
guard let request = makeGraphRequest() else {
manager.logOut()
completion?(.failure(Error.unableToInitializeGraphRequest))
return
}
_ = request.start { _, result, error in
if let e = error {
manager.logOut()
completion?(.failure(e))
} else if let result = result as? [String: String] {
completion?(.success((result)))
} else {
manager.logOut()
completion?(.failure(Error.unexpectedGraphResponse))
}
}
}
}
public struct SignInResponse {
public let accessToken: String
public let userInfo: [String: String]
public init(accessToken: String, userInfo: [String: String]) {
self.accessToken = accessToken
self.userInfo = userInfo
}
}
public enum Result<T> {
case success(T)
case failure(Swift.Error)
public typealias Completion = (Result<T>) -> Void
}

If you want to use the native button try this out:
let buttonText = NSAttributedString(string: "your text here")
facebookButton.setAttributedTitle(buttonText, for: .normal)

As of June 2017, the accepted answer updated for Swift 3 and latest version of FBSDK, the code looks like below-
#IBAction func fbLoginPressed(_ sender: Any) {
let fbLoginManager: FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if error == nil {
if let fbLoginResult = result {
if fbLoginResult.grantedPermissions != nil && fbLoginResult.grantedPermissions.contains("email"){
self.getFBUserdata()
}
}
}
}
}
func getFBUserdata(){
if FBSDKAccessToken.current() != nil{
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,name,first_name, last_name, picture.type(large), email"]).start(completionHandler: { (cnnection, result, error) in
if error == nil{
print(result ?? "Error in getFBUserdata function")
}
})
}
}

Related

fetching user details in facebook integration in swift4

I am trying to fetch the user details after integrating the FBSDK.But unfortunately I am getting an error like "Use of unresolved identifier 'FacebookCore'".If anyone helps me ,Would be great. Thankyou!
//Imported SDK
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
func fetchUserProfile()
{
let AccessToken = FBSDKAccessToken.current()
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion))
request.start({ (response, requestResult) in
switch requestResult{
case .success(let response):
print(response.dictionaryValue)
case .failed(let error):
print(error.localizedDescription)
}
})
}
You can fetch data by this way. but you need to install new latest library with pod or manually
import FBSDKLoginKit
func signInWithFB(_ forVC:UIViewController , sucess:#escaping (_ data:NSMutableDictionary) -> (Void) , falure:#escaping (_ error:NSError? , _ cancle:Bool?) -> (Void))
{
let obj_facebook = FBSDKLoginManager()
obj_facebook.loginBehavior = .native
obj_facebook.logOut()
// "user_birthday","user_gender"
obj_facebook.logIn(withReadPermissions: ["public_profile","email"], from: forVC) { (result:FBSDKLoginManagerLoginResult?, error:Error?) -> Void in
if ((error) != nil)
{
falure(error as NSError?, false)
}
else if ((result?.isCancelled) == true)
{
falure(nil, true)
}
else
{
let dicLoginData = NSMutableDictionary()
// name,email,first_name,last_name,birthday
let request = FBSDKGraphRequest(graphPath: "/me?fields=name,email,first_name,last_name", parameters: nil)
let _ =
request?.start(completionHandler: { (Connection:FBSDKGraphRequestConnection?, result:Any!, error:Error?) -> Void in
if error == nil
{
guard let results = result as? [String: Any] else {
falure(nil, false)
return
}
sucess(result)
}
else
{
falure(error as NSError?, false)
}
})
}
}
}
import FBSDKCoreKit
import FBSDKLoginKit
var facebookInfoDict = [String:AnyObject]()
var profileURL = ""
var facebookName = ""
var socialDetailDict = [String:AnyObject]()
#IBAction func loginWithFacebookAction(_ sender: UIButton) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logOut()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email", "user_friends"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
// if user cancel the login
if (result?.isCancelled)!{
return
}
if(fbloginresult.grantedPermissions.contains("email")) {
if Reachability.isConnectedToNetwork() {
self.getFBUserData()
} else {
}
}
}
}
}
//function is fetching the facebook user data
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
self.facebookInfoDict = result as! [String : AnyObject]
print(self.facebookInfoDict)
if let profileData = self.facebookInfoDict["picture"]!["data"] as? Dictionary<String, AnyObject> {
self.socialDetailDict = self.facebookInfoDict
self.profileURL = profileData["url"] as! String
self.facebookName = self.facebookInfoDict["name"] as! String
UserDefaults.standard.synchronize()
}
}
})
}
}
You can try integrating the "FacebookCore" and "FacebookLogin"
pod 'FacebookLogin'
pod 'FacebookShare'
After pod install, try this code, it's using for swift 4
import FacebookLogin
import FacebookCore
#IBAction func btnFacebookLoginTapped(_ sender: Any) {
let loginManager = LoginManager()
loginManager.logIn(readPermissions: [.publicProfile, .email], viewController: self) { (result) in
switch result {
case .success(grantedPermissions: _, declinedPermissions: _, token: _):
print("Succesfully logged in into Facebook.")
self.fetchFacebookUser()
case .failed( let error ):
print(error)
self.showAlertError(TL("warning_facebook_connect_failed"))
case .cancelled: break
}
}
}
fileprivate func fetchFacebookUser() {
let graphRequestConnection = GraphRequestConnection()
let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: .defaultVersion)
graphRequestConnection.add(graphRequest, completion: { (httpResponse, result) in
switch result {
case .success(response: let response):
print(responseDict)
case .failed( _):
}
})
graphRequestConnection.start()
}

Swift - Getting error from facebook login

just to start off with I have looked at many examples but can't seem to find a solution. Well I thought I did but it doesn't seem to work.
What I'm trying to do is use the Firebase auth login method for facebook, which works by the way. My issue is I want to link the password auth credentials and facebook auth credentials when the facebook method throws the error that the email/credentials already exist. I read here that I could use error.email but it doesn't give me the option to access the email.error. I might be missing something and I have spent a lot of time trying to figure it out.
Here is my code:
func signInFacebookUser() {
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
if let error = error {
self.errorMessagePopUp(title: "Failed to login", message: error)
return
}
guard let accessToken = FBSDKAccessToken.current() else {
print("Failed to get access token")
return
}
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
Auth.auth().signIn(with: credential, completion: { (user, error) in
if let error = error {
let errCode = AuthErrorCode(rawValue: error._code)!
switch errCode {
case .accountExistsWithDifferentCredential:
// somehow obtain email right here.
self.showSigninForm(attributes: self.attributes(), style: .light)
return
default:
return
}
}
self.performSegue(withIdentifier: "signInBtnPressed", sender: nil)
})
}
}
This Works for me Swift4
#objc func loginButtonClicked() {
var alertController = UIAlertController()
let loginManager = LoginManager()
loginManager.logIn(readPermissions: [ReadPermission.email], >viewController: self, completion: {loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(_, _, let accessToken):
print(accessToken)
}
})
}
func getDataUser() {
let request = GraphRequest(graphPath: "me?fields=first_name,last_name,email",
parameters:[:],
httpMethod: .GET)
request.start { httpResponse, result in
switch result {
case .success(let response): do {
print("Graph Request Succeeded: \(response)")
let dicval = response.dictionaryValue
let email = dicval?.keys
print("Graph Request Succeeded: \(String(describing: email))")
}
case .failed(let error):
print("Graph Request Failed: \(error)")
}
}
}
}
Try this code
#IBAction func facebookLoginButtonTapped(_ sender: UIButton) {
if Utility.checkInternet(){
let fbLoginManager: FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, error) in
if (error == nil){
let fbLoginResult: FBSDKLoginManagerLoginResult = result!
if fbLoginResult.grantedPermissions != nil {
if(fbLoginResult.grantedPermissions.contains("email")){
self.facebookLogin()
}
}
}
}
}
else{
Utility.showAlert("Connection Error!", message: "Please check internet connection and retry.", viewController: (self.window?.rootViewController)!)
}
}
func facebookLogin() {
Utility.showProgress("")
if((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, name, first_name, last_name, picture.type(large), email "]).start(completionHandler: { (connection, result, error) in
if result == nil {
}
else{
self.userResults = result as! [String : AnyObject]
self.email = (self.userResults["email"] as? String) ?? ""
if (self.email != "") {
}
else{
Utility.showAlert("Login Faild", message: error?.localizedDescription, viewController: self)
}
}
})
}
}

From FBSDKGraphRequest to Storyboard

i'm total beginner in coding, but i have a big question :)
I dont know how to get information to display on Main Storyboard form FBSDKGraphRequest.
What i should do next to get picture to Storyboard? I hope someone can help me :)
Using Swift 3, Xcode 8
Facebook login is working and code is:
func getFBUserData(){
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){
self.dict = result as? [String : AnyObject]
print(result!)
print(self.dict)
}
})
}
I see it as four steps -- my apologies to you if you already know some of this.
Drag an UIImageView from the object library in interface builder to a view in your storyboard.
Connect the UIImageView to your code as an IBOutlet and name it. Do this by control dragging from your new UIImageView to your code, and then in the popup specify an outlet and name the UIImageView. In my case I named it 'facebookPicture'
Get the URL for the image from the FaceBook result. The following sample code drills down into the result dictionary step by step. There are many ways to shorten this.
#IBOutlet var facebookPicture: UIImageView!
func getFBUserID(){
if((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(
completionHandler: {
[weak self] (connection, result, error) -> Void in
guard let strongSelf = self else { return }
if let error = error {
print("Failed to download FB user with error:. \(error)")
}
if (error == nil) {
let resultDictSwift = result as! Dictionary<String, Any>
if let picture = resultDictSwift["picture"] as? Dictionary<String, Any> {
if let pictureData = picture["data"] as? Dictionary<String, Any> {
if let pictureURL = NSURL(string: (pictureData["url"] as? String)! ) {
strongSelf.downloadImage(url: pictureURL as URL)
}
}
}
print(result!)
let FBID = resultDictSwift["id"]
strongSelf.facebookID = FBID as! String?
print("User FB id: \(strongSelf.facebookID!)")
}
})
}
}
Download the image from the URL location. The following does this asynchronously and uses a previous stack overflow answer here When the download is complete, it sets the result of the download to be the image in your UIImageView
func getDataFromUrl(url: URL, completion: #escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
completion(data, response, error)
}.resume()
}
func downloadImage(url: URL) {
print("Download Started")
getDataFromUrl(url: url) { data, response, error in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
DispatchQueue.main.async() {
self.facebookPicture.image = UIImage(data: data)
}
}
}
I hope this helps!
In Swift 3
#IBAction func btnFacebookTapped(_ sender: UIButton)
{
let loginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: ["user_about_me", "email" , "user_birthday","user_hometown"], from: self) { (loginResult, error) in
if error != nil
{
self.showalert(strMessage: (error?.localizedDescription)!)
}
else
{
if loginResult?.grantedPermissions == nil
{
self.showalert(strMessage: "Login Permissions not granted")
return
}
if (loginResult?.grantedPermissions.contains("email"))!
{
self.getFBUserData()
}
}
}
}
func getFBUserData()
{
FBSDKGraphRequest.init(graphPath: "me?fields=id,name,email,first_name,last_name,cover,picture.type(large),gender,birthday,hometown", parameters: nil).start(completionHandler: { (connection , result , error ) in
if(error == nil){
DispatchQueue.main.async {
let dictionary = result as! NSDictionary
print(dictionary)
print("Name : \(dictionary.value(forKey: "name")!)")
print("FB ID : \(dictionary.value(forKey: "id")!)")
}
}else{
self.showalert(strMessage: "Somthig Went Wrong..!")
}
})
}

Facebook Login not working in IOS SDK

I am struggling with the issue of Facebook login, Yesterday Facebook login working correctly but today when I run my app it's not working don't know why, why it's not working suddenly I already configured all things related to Facebook login in Facebook develop console everything is configured
Please help me out if you have any idea, I already enable Keychain sharing also.
Code for Facebook Login
#IBAction func onClickFacebookLoginAction(_ sender: Any) {
var message = String()
if Reachability.isConnectedToNetwork() == true{
let loginView:FBSDKLoginManager = FBSDKLoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.web
loginView.logIn(withReadPermissions: ["email","public_profile","user_friends"], from: self, handler: { (result, error) in
if(error != nil){
print("Error while try to login with facebook-\(error?.localizedDescription)")
}else if (result?.isCancelled)!{
print("User cancel the facebook login")
}else{
if result?.grantedPermissions != nil{
if (result?.grantedPermissions .contains("email"))!{
self.ShowProgressHUD()
self.fetchUserInfo()
}else{
message = message.appending("Facebook email permission error")
self.showAlertMessage(message: message, title: "")
}
}
}
})
}
}
func fetchUserInfo() -> Void {
if (FBSDKAccessToken.current() != nil) {
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/me", parameters:["fields": "id, first_name, last_name, name, email, picture"])
graphRequest.start(completionHandler: { (connection, result, error) in
if(error != nil){
self.showAlertMessage(message: (error?.localizedDescription)!, title: "")
}
else
{
print("Result is:\(result)")
self.dictionary = result as! [String : AnyObject]
let name = self.dictionary["name"] as!String
let email = self.dictionary["email"] as! String
let token = FBSDKAccessToken.current().tokenString
print("name is -\(name)")
print("email is -\(email)")
print("token is -\(token)")
DispatchQueue.main.async {
let SelectionViewObj = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(SelectionViewObj, animated: true)
}
}
})
}
}
This is the code i am using to authenticate facebook.its working like a charm.Just check or replace with this.
self.fbLoginManager = FBSDKLoginManager.init()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.web
self.fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil) {
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil && fbloginresult.grantedPermissions.contains("email") {
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, first_name, last_name, email, gender, birthday, location"]).start(completionHandler: { (connection, result, error) -> Void in
if error != nil {
print(error?.localizedDescription ?? "error in facebook login...!")
return
}
if let dict = result as? NSDictionary {
print("facebook login result --->\n\(dict)")
guard let id = dict["id"] else {
print("Ooops... id = nil")
return
}
// let firstName: String = dict["first_name"] as? String ?? ""
// let lastName : String = dict["last_name"] as? String ?? ""
// let email : String = dict["email"] as? String ?? ""
self.fbLoginManager.logOut()
}
})
}
} else {
print("facebook ---> login canceled")
}
} else {
print(error?.localizedDescription ?? "facebook login has error")
}
}
This is how I used to log in with Facebook,
#IBAction func act_loginFB(_ sender: UIButton) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email",], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
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)
let data: [String:AnyObject] = (result as? [String : AnyObject])!
print(data)
let email: String = data["email"]! as! String
print(email)
}
})
}
}
Check you added the correct FacebookAppID and FacebookDisplayName in the info.plist

iOS facebookSDK get user full details

Iam using the last FBSDK (using swift)
// MARK: sign in with facebook
func signInWithFacebook()
{
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
println("already logged in ")
self.returnUserData()
return
}
var faceBookLoginManger = FBSDKLoginManager()
faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error)-> Void in
//result is FBSDKLoginManagerLoginResult
if (error != nil)
{
println("error is \(error)")
}
if (result.isCancelled)
{
//handle cancelations
}
if result.grantedPermissions.contains("email")
{
self.returnUserData()
}
})
}
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("the access token is \(FBSDKAccessToken.currentAccessToken().tokenString)")
var accessToken = FBSDKAccessToken.currentAccessToken().tokenString
var userID = result.valueForKey("id") as! NSString
var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
println("fetched user: \(result)")
}
when I print the fetched user I get only the id and the name ! ,
but i requested a permission for email and friends and profile ,
what's wrong ???
BTW : I moved this project from my macbook to another macbook ( because I formatted mine) it worked very well when it was at the the macbook which I created the project on , but after moving the project (using bitbucket clone) I got this results .
As per the new Facebook SDK, you must have to pass the parameters with the FBSDKGraphRequest
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
Documentations Link : https://developers.facebook.com/docs/facebook-login/permissions/v2.4
User object reference : https://developers.facebook.com/docs/graph-api/reference/user
With public profile you can get gender :
public_profile (Default)
Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default:
id
name
first_name
last_name
age_range
link
gender
locale
timezone
updated_time
verified
Swift 4
An example in Swift 4 that also shows how to correctly parse out the individual fields from the result:
func fetchFacebookFields() {
//do login with permissions for email and public profile
FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: nil) {
(result, error) -> Void in
//if we have an error display it and abort
if let error = error {
log.error(error.localizedDescription)
return
}
//make sure we have a result, otherwise abort
guard let result = result else { return }
//if cancelled nothing todo
if result.isCancelled { return }
else {
//login successfull, now request the fields we like to have in this case first name and last name
FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name"]).start() {
(connection, result, error) in
//if we have an error display it and abort
if let error = error {
log.error(error.localizedDescription)
return
}
//parse the fields out of the result
if
let fields = result as? [String:Any],
let firstName = fields["first_name"] as? String,
let lastName = fields["last_name"] as? String
{
log.debug("firstName -> \(firstName)")
log.debug("lastName -> \(lastName)")
}
}
}
}
}
I guess this code should help you get the required details
Swift 2.x
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
print("User Name is: \(userName)")
let userID : NSString = result.valueForKey("id") as! NSString
print("User Email is: \(userID)")
}
})
In Swift 4.2 and Xcode 10.1
#IBAction func onClickFBSign(_ sender: UIButton) {
if let accessToken = AccessToken.current {
// User is logged in, use 'accessToken' here.
print(accessToken.userId!)
print(accessToken.appId)
print(accessToken.authenticationToken)
print(accessToken.grantedPermissions!)
print(accessToken.expirationDate)
print(accessToken.declinedPermissions!)
let request = GraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,first_name,last_name,picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue!)
case .failed(let error):
print(error)
}
}
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
self.present(storyboard, animated: true, completion: nil)
} else {
let loginManager=LoginManager()
loginManager.logIn(readPermissions: [ReadPermission.publicProfile, .email, .userFriends, .userBirthday], viewController : self) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in : \(grantedPermissions), \n \(declinedPermissions), \n \(accessToken.appId), \n \(accessToken.authenticationToken), \n \(accessToken.expirationDate), \n \(accessToken.userId!), \n \(accessToken.refreshDate), \n \(accessToken.grantedPermissions!)")
let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue!)
case .failed(let error):
print(error)
}
}
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
self.navigationController?.pushViewController(storyboard, animated: true)
}
}
}
}
https://developers.facebook.com/docs/graph-api/reference/user

Resources