Facebook Login – Swift Language – iOS 10 – Swift 3

Facebook Login using custom button in Swift Language

Xcode Version 8

Article is Updated with the Facebook 4.16 SDK [27-September-2016].

facebook-logo

Install Pods

Appdelegate.swift

ViewController.swift

Create touch up inside event for custom button like as follows

In iOS 10 don’t forget to set capabilities (Keychain Sharing) :

Go to your project targets -> Capabilities -> Keychain Sharing -> Toggle Switch ON

Capabilities - KeyChain Sharing
Capabilities – KeyChain Sharing

Detailed Output Log :

Happy Coding 😀

  • Hi,

    I have used the same code in my app. But when i stop the app and run again the
    FBSDKAccessToken.currentAccessToken() is null in the viewdidload method. Can u suggest any fix

    • Just don’t logout after login first time. When you want to logout then and then use the logout method of LoginManager.

      Thanks!

  • Thank you.

  • Alex

    Thank you so much! Spent tons of time – nothing worked, but your code 100% great and all works fine! Thank you again 🙂

  • Hey,

    First of all, thank you. It works! And just like everyone else I was looking for this for quite a while.

    I’m trying to integrate this code with Parse. Do you have any resources or suggestions on that?

    Thank you,

    Jonathan

    • I have not used Parse yet. But surely you will find the resources at Stack Overflow. There are many question/answers with the Parse and Facebook.

      Thank you,
      Ashish Kakkad

  • Bikram

    var fbloginresult : FBSDKLoginManagerLoginResult = result

    if(fbloginresult.grantedPermissions.contains(“email”)) -> here in this line it says bad access , here is no permissions for email.

    fbloginresult contains nothing.. empty …. could you suggest something??

    Thanks in advance.

  • ravindra

    Great work ashish
    thank you

  • ankit paliwal

    thanks

  • Ivan

    How to perform the logout process ?

    • From login manager you can logout.

      var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
      fbLoginManager.logOut()
      
      • Ivan Putera Pratama

        I already use my facebook account to log in, it succeed..
        But when i tried to log in again, in the web browser that appear showed text like this “You have already authorized (My app name)”.

        Then i try to log out with your code above.
        But when i try to do log in again it still happen (I’m not suceessfully log out).

        Please help 🙁 I appreciate your code btw 🙂

        • Logout method only logged out from an fb session with your application.
          You have to manually logout from safari or any other browser.

          Thanks

          • Ivan Putera Pratama

            Oke it solved my problem, thanks for your help 🙂

  • B Mehta

    Hi, how do I dismiss the browser tab that is shown for logging in. I receive the error and completion callbacks but the browser window is not dismissed.

    • Check your URL schemes in your project. It should have callback to your application.

  • Zak

    Hi!!
    That s really helpful. One question,
    I am not on swift, and i am having big problem to set it on objective C.
    Any chance for a little help?
    I actually set up the latest FBSDK, get to login and out but the picture and user informations don t follow the in and out logging process.
    Thank You So Much.

  • Ashok Chennuru

    hello ashish!!!
    i done same as above and when closed and reopened the app its showing already authorized. i have selected ok and click on done, i need to fetch FBUSer Details using accesstoken. But the Current AccessToken also showing nil. Can u help

    • I think you have called logout method of manager.

  • Ben Swinford

    Hi Ashok,

    When the Facebook prompt comes up and says “you have already authorized this app”, and the user hits “okay” the screen doesn’t change and nothing happens. It’s supposed to close the Facebook prompt screen automatically. Do you know why this might be?

    Also, if the user hits the “done” button in the top-left it counts the login as cancelled which is no good.

    Thanks,
    Ben

    • Ashish Kakkad

      You have to add a flag in capabilities. I will update blog soon.

      • Zane Campbell

        after adding the capability flag i still have the exact same issue.

        UPDATE: it was because i did not add the last function in AppDelegate. It does not auto complete so i thought it was not necessary. I copied and pasted it from your code and it worsks. Thank you.

    • Roberto

      I have the same issue also switching ON in the Keychain Sharing

    • Akshay Thakur

      go to developers.facebook.com, go to app review and set Make (your_app_name) public? to yes.

    • Muhammad Rizwan Mughal

      Yes !! did u set callback URL in Open URL and DidFinishLaunching Methods of Appdelegate class?

  • Swift Taylor

    hi

    Need to on keychain Sharing Key in project capabilities
    It is necessary otherwise occures error

    In xcode Targets –> Capabilities –> Keychain Sharing –> Taoggle switch ON

    Otherwise It’s really helpful

    Thank you

    • Ashish Kakkad

      Yes. I forgot to add it in this post. Thanks. I have updated this post.

  • neeta

    hi ashish,
    that was really helpful.
    thanks.

    i want to make user post on my fb page. when button is clicked, user logs in and facebook prompt for posting permission comes up and gets posted on my fb page. could there be any help in this?
    thanks

  • ankit bisht

    Hi guys. I am still not able to get the email from this code. I have been trying for longer for fetching email. I am sending you screenshot. plz analyze.. I am getting the output except email.

    https://uploads.disquscdn.com/images/d1ec540ede2f08712df15cdf72fa7c984e6b156c2b69f5ac11dec7364eb2663c.png

    • Asha Sharma

      Hi Ankit,

      You have posted this query regarding unable to get the email address from this code.

      I wish to know whether you have solved it now and is yes whats the code and way out.

      please help urgently.

  • Amanpreet

    Really Helpful post. I must appreciate . Thanks!

  • Asha Sharma

    Hi Ashish,

    Thanks for your post. Its really help full.
    But I have not received email address in getFBUserData() method.

    Please help me how to get email address and phone number from facebook login.

  • Florian Marcu

    Thanks for sharing this! It’s amazing how useful this is, a couple of years after it was published. Following these instructions, I’ve created my own Login Screen, to which I’ve added Twitter integration as well. It’s based on the latest Facebook & Twitter SDKs, so I thought to publish it to everyone iosapptemplates.com/templates/login-screen-in-swift-3-freebie