Hot on the tail of the iOS 10 release, Apple also updated Swift to version 3 and Xcode to 8.0. Facebook’s SDK for Swift can be a little tricky to set up with all this so here’s a quick run-through of all the things I am aware of at this time.

The Facebook Swift SDK

First of all, I’ve used Cocoapods to import the Facebook SDK so my Podfile includes ‘FacebookCore’, ‘FacebookLogin’ and ‘FacebookShare’. There’s plenty of examples out there for setting this up if you are unfamiliar with Cocoapods and I highly recommend it as a way to manage all your imports so they can be easily updated.

Facebook have been slow to update their documentation for the latest release and it’s not always clear which version they are referring too but you can find their examples here – https://developers.facebook.com/docs/swift

First up, let’s look at the AppDelegate

Obviously

import FacebookCore

Then the following functions

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

And

public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
   return SDKApplicationDelegate.shared.application(application, open:url, sourceApplication:sourceApplication, annotation:annotation)
}

This is simplified code specifically related to the Facebook SDK, you will likely have further content in the first function above. The second function is what allows the Facebook login window to close and return to your application. This seems to be a common problem for people as it’s missed (at time of writing) from the documentation.



To create a login button for your app that runs the Facebook login process you can use the following code:

let loginButton = LoginButton(readPermissions: [.publicProfile,.email])
loginButton.frame = CGRect(x: 20, y: view.frame.height - 190, width: view.frame.width - 40, height: 50)
loginButton.delegate = self
view.addSubview(loginButton)

Wherever you use the loginButton as above, don’t forget to import the FacebookLogin library at the top of the file. Your class will also need to be of type ‘LoginButtonDelegate’. This will mean adding two further functions to that file:

func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) {
   switch result {
   case .failed(let error):
      print(error)
   case .cancelled:
      print("Cancelled")
   case .success(let grantedPermissions, let declinedPermissions, let accessToken):
      print("Logged In")
   }
}
func loginButtonDidLogOut(_ loginButton: LoginButton) {
   print("Logged Out")
}

For the purposes of my application it made sense to roll a custom function to retrieve the data I was after at any point. It looks a little something like this:

func facebookLogin() {
   if let accessToken = AccessToken.current {
      let params = ["fields":"name,email"]
      let graphRequest = GraphRequest(graphPath: "me", parameters: params)
      graphRequest.start { (urlResponse, requestResult) in
         switch requestResult {
         case .failed(let error):
            print(error)
         case .success(let graphResponse):
            if let responseDictionary = graphResponse.dictionaryValue {
               UserDefaults.standard.set(responseDictionary, forKey: "userInfo")
            }
         }
      }
   } else {
   }
}

This is a basic call to retrieve the logged in users name and email address from the graph. You can make your call more complex if you have further requirements and obviously use the data you retrieve in any way.

I hope this is useful for anyone who, like me, struggled to get this latest series of updates working for release into the App Store. You can keep up to date with the development of the Facebook Swift SDK on their GitHub page – https://github.com/facebook/facebook-sdk-swift

As a final note, please be aware that it would seem that Apple will reject an App that requires Facebook Login. I provided an option to skip login (with reduced functionality) and that was no problem, but this may not be an option for you so it’s worth considering the options available as an alternative, most likely rolling you own login system using the users email address and a custom password.