Here we are again, back on SFSafariViewController with the Facebook login integration this time! Facebook is getting ready for iOS 9 and got its SDK updated with very few changes on our side, yay! It is still in beta version as we speak but it’s working like a charm.
Let’s see how to implement our Facebook login with SFSafariViewController in 3 easy steps.
- First, make sure you use the right SDK (the beta one):
// In your Podfile pod 'FBSDKCoreKit', :git => 'https://github.com/facebook/facebook-ios-sdk.git', :branch => 'release-4.6.0-beta1' pod 'FBSDKLoginKit', :git => 'https://github.com/facebook/facebook-ios-sdk.git', :branch => 'release-4.6.0-beta1' pod 'FBSDKShareKit', :git => 'https://github.com/facebook/facebook-ios-sdk.git', :branch => 'release-4.6.0-beta1'
- Then, follow the Facebook instructions.
You probably have all that already set up minus the iOS 9 migration. It should be an easy step.
- Getting started
- Preparing Your Apps for iOS9
- Facebook login
At that point, you don’t even need step 3 if you followed Facebook’s instructions, especially step 6: “Connect Application Delegate”
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation ]; }
But, if you want to keep it consistent with my previous post, follow the last step.
// in your ApplicationDelegate func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { if (sourceApplication == "com.apple.SafariViewService") { NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url) // test not mandatory, it's just to make the difference between potential returns if (url.absoluteString.containsString("fb\(FACEBOOK_APP_ID)://authorize")) { // Facebook return is the key point here return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) } return true } return true }
- Finally, make sure you use the Facebook return in the openURL function as follows:
// in your ApplicationDelegate func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { if (sourceApplication == "com.apple.SafariViewService") { NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url) // test not mandatory, it's just to make the difference between potential returns if (url.absoluteString.containsString("fb\(FACEBOOK_APP_ID)://authorize")) { // Facebook return is the key point here return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) } return true } return true }
Build and run, et voilà, you can now login to your app with Facebook on the brand new SFSafariViewController.
No more app jumping and confusing configs, everything has been taken care of and it’s all Apple compliant. Sweet!
Just one word about the other social network fellows:
- Twitter: Fabric now handles everything (Login via Twitter, Crashlytics, MoPub, Digits). The integration in Xcode is as easy as their website is confusing. Don’t be surprised.
- Google: well, there is a new version of the SDK. So exit the well-known hack a lot of us used. There is now a proper way to log in via Google on iOS. Let’s hope it will be approved by Apple as it doesn’t use the shiny SFSafariViewController (sob).
Quick reminder in case you missed it: Xcode 7 beta 6 is out since last week ! It’s the last beta version before the official release on September 9. You can download it here and have a look at the release notes too.
Happy coding :)