Connect Facebook to an app delegate in Swift 4

Viewed 4086

I think it is a very easy part, but I can`t understand..

I see "https://developers.facebook.com/docs/facebook-login/ios" and I want to add Facebook login to the project.

However, adding the source code to the App Delegate is difficult.

I added it this simple, but it does not work.

import FBSDKCoreKit

class AppDelegate: UIResponder, UIApplicationDelegate 
{
    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [[FBSDKApplicationDelegate sharedInstance] 
        application:application
        didFinishLaunchingWithOptions:launchOptions];

        return YES;
    }

    - (BOOL)application:(UIApplication *)application
    openURL:(NSURL *)url
    options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
    {
        BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
        openURL:url
        sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
        annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
        ];

        return handled;
    }
}

Please tell me where to put the source code correctly.

4 Answers

Swift 5 This is also for Facebook's current SDK's as of posting date. I would link the Facebook Swift documentation but it hasn't been updated, as SDKApplicationDelegate has been changed to ApplicationDelegate.

In your didFinishLaunchingWithOptions add this line:

let _ = ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

And below the didFinishLaunchingWithOptions function, add this function:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    return ApplicationDelegate.shared.application(app, open: url, options: options)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let appId: String = SDKSettings.appId
         if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host ==  "authorize" {
              return SDKApplicationDelegate.shared.application(app, open: url, options: options)
         }
    return false
}

For more info :-enter link description here

First of all, do not write the Objective C code in the Swift project. Its a pure mistake of programming for iOS.

Connect Your App Delegate

Add the following to your AppDelegate class. This initializes the SDK when your app launches, and lets the SDK handle results from the native Facebook app when you perform a Login or Share action.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return SDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
}

Recommended

For installation of Facebook in Swift project please follow

https://developers.facebook.com/docs/swift/getting-started

Related