Swift 1.2 and Facebook’s New Login SDK

I’ve had the pleasure of working with Apple’s new Swift over the last few months, and recently ran into some problems integrating Facebook’s API with an iOS 8 app I’m building.

The problem I was having is that, immediately after login, I couldn’t seem to get the user’s access token.

[[code]]czoxMzc6XCI8c3BhbiBjbGFzcz1cInR5cFwiPkZCU0RLQWNjZXNzVG9rZW48L3NwYW4+PHNwYW4gY2xhc3M9XCJwdW5cIj4uPC9zcGFuPjxze1smKiZdfXBhbiBjbGFzcz1cInBsblwiPmN1cnJlbnRBY2Nlc3NUb2tlbjwvc3Bhbj48c3BhbiBjbGFzcz1cInB1blwiPigpPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

was always returning nil, even with the user logged in.

You need to get this token so that you can send it to your Rails app (or whatever you’re using on the back end) and perform Facebook authentication on behalf of your user up there.

My viewDidLoad looked like this:

[[code]]czoyNjk2OlwiPHNwYW4gY2xhc3M9XCJrd2RcIj5vdmVycmlkZTwvc3Bhbj48c3BhbiBjbGFzcz1cInBsblwiPiBmdW5jIHZpZXdEaWRMb2Fke1smKiZdfTwvc3Bhbj48c3BhbiBjbGFzcz1cInB1blwiPigpPC9zcGFuPiA8c3BhbiBjbGFzcz1cInB1blwiPns8L3NwYW4+DQogICAgPHNwYW4gY2xhe1smKiZdfXNzPVwia3dkXCI+c3VwZXI8L3NwYW4+PHNwYW4gY2xhc3M9XCJwdW5cIj4uPC9zcGFuPjxzcGFuIGNsYXNzPVwicGxuXCI+dmlld0RpZExvYWR7WyYqJl19PC9zcGFuPjxzcGFuIGNsYXNzPVwicHVuXCI+KCk8L3NwYW4+DQogICAgPHNwYW4gY2xhc3M9XCJjb21cIj4vLyBEbyBhbnkgYWRkaXRpb257WyYqJl19YWwgc2V0dXAgYWZ0ZXIgbG9hZGluZyB0aGUgdmlldywgdHlwaWNhbGx5IGZyb20gYSBuaWIuPC9zcGFuPjxzcGFuIGNsYXNzPVwicGx7WyYqJl19blwiPg0KICAgIHByaW50bG48L3NwYW4+PHNwYW4gY2xh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KICAgIHtbJiomXX0gICAgbG9naW5WaWV3PC9zcGFuPjxzcGFuIGNsYXNzPVwicHVuXCI+Ljwvc3Bhbj48c3BhbiBjbGFzcz1cImt3ZFwiPmRlbGVnYXRlPC9zcHtbJiomXX1hbj4gPHNwYW4gY2xhc3M9XCJwdW5cIj49PC9zcGFuPiA8c3BhbiBjbGFzcz1cImt3ZFwiPnNlbGY8L3NwYW4+DQogICAgPHNwYW4gY2xhc3tbJiomXX1zPVwicHVuXCI+fTwvc3Bhbj4NCjxzcGFuIGNsYXNzPVwicHVuXCI+fTwvc3Bhbj5cIjt7WyYqJl19[[/code]]

I used Brian Coleman’s helpful tutorial to get started, but it must not be updated for the latest SDK, because I followed it to a T and was definitely not getting the token.  Here’s what I needed to change:
[[code]]czoyNTA6XCIvL2FkZGVkIHRoZSBmb2xsb3dpbmcgbGluZSBpbiBteSB2aWV3RGlkTG9hZCANCjxzcGFuIGNsYXNzPVwidHlwXCI+RkJTRHtbJiomXX1LUHJvZmlsZTwvc3Bhbj48c3BhbiBjbGFzcz1cInB1blwiPi48L3NwYW4+PHNwYW4gY2xhc3M9XCJwbG5cIj5lbmFibGVVcGRhdGVzT25BY3tbJiomXX1jZXNzVG9rZW5DaGFuZ2U8L3NwYW4+PHNwYW4gY2xhc3M9XCJwdW5cIj4oPC9zcGFuPjxzcGFuIGNsYXNzPVwia3dkXCI+dHJ1ZTwvc3BhbntbJiomXX0+PHNwYW4gY2xhc3M9XCJwdW5cIj4pPC9zcGFuPg0KXCI7e1smKiZdfQ==[[/code]]

Created a callback function to handle the updating of the FBSDKAccessToken state:
    // in a LoginViewController.swift
    func onTokenUpdated(notification: NSNotification) {
        println(FBSDKProfile.currentProfile().name)
        if ((FBSDKAccessToken.currentAccessToken()) != nil) {
            println("token is not nil ")
        } else {
            println("token is nil")
        }
    }

finally, in my viewDidLoad function, I added:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKAccessTokenDidChangeNotification, object: nil)

And this did it! Hope this helps you! Leave a comment if you have any questions.

5 thoughts on “Swift 1.2 and Facebook’s New Login SDK”

  1. Hi Andrew! .. Thank you for the useful post.

    Just a note… on your last code snippet, I think the selector should read: “onToeknUpdated:” right?

  2. Thanks for the post Andrew – it didn’t do the trick without changes

    I made changes:
    1. changed observer to FBSDKProfileDidChangeNotification
    2 changed name of method to match selector ( probably a typo)
    func onProfileUpdated(notification: NSNotification)

    nice one..cheers for the help

Leave a Reply

Your email address will not be published. Required fields are marked *