PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, June 29, 2022

[FIXED] How to print the public details like first name, last name or email in Facebook SDK 4.18.0 in swift3?

 June 29, 2022     facebook-sdk-4.0, ios, swift3     No comments   

Issue

I am integrating facebook login in Swift3 language in iOS using FBSDK4.18.0.Below is the following code: But it does not prints the results including

     func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        print("User Logged In")
        if ((error) != nil)
        {
            // Process error
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if result.grantedPermissions.contains("public_profile")
            {
                // Do work
//                let strFirstName: [String: Any]? = (result.objectForKey("first_name") as? String)!
//                let strLastName: String = (result.objectForKey("last_name") as? String)!
//                let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!

                if let dic = result as? [String: Any]
                { if let strFirstName = dic["first_name"] as? String, let strLastName = dic["last_name"] as? String
                {
                    print("\(strFirstName) \(strLastName)")
                    }
                }
            }
        }
    }

It also shows the "Cast from 'FBSDKLoginManagerLoginResult!' to unrelated type '[String : Any]' always fails" warning at "if let dic = result as? [String: Any]"


Solution

You should use AnyObject and not Any. More on it can be found here:

if let dic = result as? [String: AnyObject] { 
   if let strFirstName = dic["first_name"] as? String, let strLastName = dic["last_name"] as? String
       {
          print("\(strFirstName) \(strLastName)")
       }
 }


Answered By - Nikos M.
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing