Steps: Open this link

 developer.facebook:https://developers.facebook.com/docs/ios/getting-started  and  after tap  Quick start for iOS on this page and follow all steps .

In xCode do the follwing:

  1. In these lines in podfile and Run install podfile.
  pod 'FBSDKCoreKit', '~> 4.15.1'
  pod 'FBSDKShareKit', '~> 4.15.1'
  pod 'FBSDKLoginKit', '~> 4.15.1'

2. In project setting make:

Always Embed Swift Standard Libraries  to YES

3. In  AppDelegate, add these:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
      FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        return true

    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

        return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }

4. In your viewController.swift add :

//MARK: Button Action

    @IBAction func tapLogin(_ sender: Any) {
        let loginManagerFB : FBSDKLoginManager = FBSDKLoginManager()
        loginManagerFB.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if (error == nil){
                let fbloginResult : FBSDKLoginManagerLoginResult = result!
                if fbloginResult.grantedPermissions != nil {
                    if(fbloginResult.grantedPermissions.contains("email"))
                    {
                        self.getUserDetails()
                        loginManagerFB.logOut()
                    }
                }
            }
        }
    }

    //MARK: Get FB user details
    func getUserDetails(){
        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.dictUserData = result as! [String : AnyObject]
                    print(self.dictUserData)
                    print(result!)
                }
            })
        }
    }

5. output in log is like :

["picture": {
    data =     {
        "is_silhouette" = 0;
        url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14067503_883483488422704_7702111205876496057_n.jpg?oh=0cfcb8e6f38242e549844a444cda7c86&oe=596F938";
    };
}, "name": Båibhav Singh, "last_name": Singh, "email": xyz@hotmail.com, "id": 1061571467280571, "first_name": Båibhav]
{
    email = "xyz@hotmail.com";
    "first_name" = "B\U00e5ibhav";
    id = 1061571467280571;
    "last_name" = Singh;
    name = "B\U00e5ibhav Singh";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14067503_883483488422704_7702111205876490057_n.jpg?oh=0cfcb8e6f38242e549844a444cda7c86&oe=596F933F";
        };
    };
}
Advertisements