将Facebook连接到Swift 4中的应用程序委托

时间:2018-10-30 06:22:54

标签: ios swift facebook swift4 xcode10

我认为这是一个非常简单的部分,但是我听不懂。

我看到“ https://developers.facebook.com/docs/facebook-login/ios”,我想将Facebook登录名添加到项目中。

但是,很难将源代码添加到App Delegate中。

我简单地添加了它,但是它不起作用。

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;
    }
}

请告诉我正确放置源代码的位置。

4 个答案:

答案 0 :(得分:1)

首先,不要在Objective C项目中编写Swift代码。为iOS进行编程纯属错误。

连接您的应用程序代表

将以下内容添加到您的AppDelegate类中。这会在应用启动时初始化SDK,并让SDK在执行“登录”或“共享”操作时处理来自本地Facebook应用的结果。

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

推荐

要在Swift项目中安装Facebook,请遵循

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

答案 1 :(得分:1)

迅速5 截止发布日期,这也适用于Facebook当前的SDK。我会链接Facebook Swift文档,但由于SDKApplicationDelegate已更改为ApplicationDelegate,因此它尚未更新。

在您的didFinishLaunchingWithOptions中添加以下行:

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

在didFinishLaunchingWithOptions函数下方,添加以下函数:

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

答案 2 :(得分:0)

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
}

有关更多信息:-enter link description here

答案 3 :(得分:0)

返回Facebook文档“ https://developers.facebook.com/docs/facebook-login/ios”,并为Swift使用代码,这里您已在Swift项目中使用了Objective C代码。