0

I'm trying to learn something about swift and facebook. I integrated the SDK manually without using the pods and so far everything is ok! I created a LogIn button by following some online guides and this works too! Through this code located in the ViewController I can also print Id, First_Name, Last_Name and receive the ProfilePicture info :

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if (error == nil) {
            print("Connected")
            let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")

            r?.start(completionHandler: { (test, result, error) in
                if(error == nil)
                {
                    print(result as Any)
                }
            })
        } else
        {
            print(error.localizedDescription)
        }
    }
    func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -> Bool {
        return true
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Disconnected")
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let loginButton = FBSDKLoginButton()
        loginButton.readPermissions = ["public_profile", "email"]
        loginButton.center = self.view.center
        loginButton.delegate = self
        self.view.addSubview(loginButton)
    }


}

The OutPut is this :

Optional({
    email = "cxxxxxxxxx2@live.it";
    "first_name" = Cxxxxxe;
    id = 10xxxxxxxxxxxxx75;
    "last_name" = Lxxxxxo;
    picture =     {
        data =         {
            height = 200;
            "is_silhouette" = 0;
            url = "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10xxxxxxxxxxxxx75&height=200&width=200&ext=1565467901&hash=AeQ-NalWNEMh91CK";
            width = 200;
        };
    };
})

Now I don't know how to extract a single information such as the first_name in a variable to be able to print it in a label or take the url of the image to print it in the app.

if I try to change inside the login function like this :

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if (error == nil) {
            print("Connected")
            let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")

            r?.start(completionHandler: { (test, result, error) in
                if(error == nil)
                {
                    print(result!["first_name"] as Any)
                }
            })
        } else
        {
            print(error.localizedDescription)
        }
    }

the compiler tells me : "Value of type 'Any' has no subscripts".

How can i do this?

Thanks ;)

Charlie Lomello
  • 67
  • 2
  • 15

3 Answers3

0

in the result you should put

result.user 

and that object is the one that contains the properties like displayName, or firstName for this matter

Samuel Chavez
  • 768
  • 9
  • 12
0

I created the labels and for now I solved it this way:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if (error == nil) {
        print("Connected")
        let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")

        r?.start(completionHandler: { (test, result, error) in

            if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
                print("id: \(id)")
                self.lbl_fbid.text = "ID: \(id)"
            }
            if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
                print("first_name: \(first_name)")
                self.lbl_fbdirstname.text = "First_Name: \(first_name)"
            }
            if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
                print("last_name: \(last_name)")
                self.lbl_fblastname.text = "First_Name: \(last_name)"
            }
            if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
                print("email: \(email)")
                self.lbl_fbemail.text = "Email: \(email)"
            }
        })
    } else
    {
        print(error.localizedDescription)
    }
}

Now I just have to figure out how to take the profile pic...

Charlie Lomello
  • 67
  • 2
  • 15
0

All done... if someone need the code i wrote this:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

        if (error == nil) {
            print("Connected")
                        let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
                        r?.start(completionHandler: { (test, result, error) in
                        if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
                            print("id: \(id)")
                            self.lbl_fbid.text = "ID: \(id)"
                        }
                            if let imageURL = (((((result! as AnyObject).value(forKey: "picture")) as AnyObject).value(forKey: "data")) as AnyObject).value(forKey: "url") as? NSString{
                            print("img url: \(imageURL)")
                            let url = URL(string: imageURL as String)
                            DispatchQueue.global().async {
                                let data = try? Data(contentsOf: url!)
                                DispatchQueue.main.async {
                                    self.img_fbprofilepic.image = UIImage(data: data!)
                                }
                            }
                        }
                        if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
                            print("first_name: \(first_name)")
                            self.lbl_fbdirstname.text = "First_Name: \(first_name)"
                        }
                        if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
                            print("last_name: \(last_name)")
                            self.lbl_fblastname.text = "First_Name: \(last_name)"
                        }
                        if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
                            print("email: \(email)")
                            self.lbl_fbemail.text = "Email: \(email)"
                        }

                            if(error == nil)
                            {
                                print(result as Any)
                            }

                        })

        } else  {
            print(error.localizedDescription)
        }
    }
Charlie Lomello
  • 67
  • 2
  • 15