Daniel Que Daniel Que - 1 month ago 42
Swift Question

Handling open url for Facebook Login with Swift SDK

I'm using the new Facebook Swift SDK instead of the Objective C SDK.

Previously, the

openURL
in
AppDelegate.swift
had to be implemented. However, Facebook's Swift tutorial doesn't cover this part.

The following is logged:

Implementation of application:openURL:sourceApplication:annotation: not found. Please add the handler into your App Delegate. Class: Shoppie.AppDelegate


But my implementation doesn't work:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: nil)
if (url.scheme?.hasPrefix("fb"))! {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: nil)
} else {
return GIDSignIn.sharedInstance().handle(url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: nil)
}
return false
}


Due to:

Use of unresolved identifier 'FBSDKApplicationDelegate'


I installed the Swift SDK by adding the following to my
Podfile
:

pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'


And running
pod install
.

I can't find what the updated code should be.

Answer

Their documentation is a bit lacking, but they do provide a good sample app in their github repository. see https://github.com/facebook/facebook-sdk-swift/blob/master/Samples/Catalog/Sources/AppDelegate.swift

Here's the code (Swift 3) I'm using to intergate the SDK into my apps:

import FacebookCore

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

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

    return true
  }

  func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

    if let _ = Bundle.main.object(forInfoDictionaryKey: "FacebookAppID") as? String {
      return SDKApplicationDelegate.shared.application(application,
                                                       open: url,
                                                       sourceApplication: sourceApplication,
                                                       annotation: annotation)
    }

      return false
    }

  @available(iOS 9.0, *)
  func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {

    if let _ = Bundle.main.object(forInfoDictionaryKey: "FacebookAppID") as? String {
      return SDKApplicationDelegate.shared.application(application, open: url, options: options)
    }

      return false
  }
}

FYI - the facebook swift library is basically just a wrapper to their standard objective-c library.