Bri Manning

Swift 3.0, AppDelegate, Social Logins, and Incorrect Documentation

November 17, 2016

I’ve been working on a small side-project to get more acquainted with Swift. The iOS and Android apps aren’t ready yet. It’s not much, but the goal of Type is to have a place where you can publish tweets and Facebook statuses at the same time. It’s not original, and there are plenty of other apps that do the same or more, it’s just to help myself learn some new things.

What I’ve found is that with the Swift language updating so quickly, the examples and documentation for both Facebook and Twitter are not quite right – and don’t work together.

Here is the code for AppDelegate.swift I created to get login working properly after poking around for half a day.

    [...]
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        Fabric.with([Crashlytics.self, Twitter.self])
        return true
    }
    
    func application(_ application: UIApplication, openURL url: URL, options: [String : AnyObject]) -> Bool {
        if Twitter.sharedInstance().application(application, open: url, options: options) {
            return true
        }
        
        let sourceApplication: String? = options["UIApplicationOpenURLOptionsSourceApplicationKey"] as? String
        return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: nil)
    }
    [...]

The didFinishLaunchingWithOptions function is straightforward and had no issues. The function to open a url-specified resource on the other hand? Documentation for Twitter and Facebook disagreed (and XCode suggested fixes on top of that). But, after digging around in code and on StackOverflow, I managed to find the above solution. I have the full file in a Gist.

Note: This isn’t a guide and doesn’t include Info.plist, cocoapods installation, or app creation, etc. This is purely some notes about AppDelegate.swift in Swift 3.0.