Facebook 与 ios 的集成失败,并在模拟器上显示错误消息

Facebook Integration with ios fails with error message on simulator

当我在模拟器上点击 continue with FaceBook 按钮时与 facebook 集成后,它会在模拟器上弹出这个 window ..当我点击返回主页时它重定向我登录 window 并允许我在 facebook 中滚动而不是重定向回我的应用程序,而且我没有得到任何用户数据。

这是我完成的步骤

  1. Pod 安装:pod 'FBSDKLoginKit'
  2. 在 developer.facebook
  3. 的快速入门指南中添加了 Bundle ID
  4. 启用单点登录选项被选择为
  5. 在 info.plist 中添加了以下代码

AppDelegate

中添加了以下代码

根据 Facebook 文档,我们必须 在 AppDelegate 中导入 FacebookCore 当我们导入它时,我们会收到警告 没有这样的模块 'FacebookCore'

import FBSDKCoreKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        ApplicationDelegate.shared.application(
                    application,
                    didFinishLaunchingWithOptions: launchOptions
                )
        return true
    }

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

SceneDelegate

中添加了以下代码

根据 Facebook 文档,我们必须 在 SceneDelegate 中导入 FacebookCore 当我们导入它时,我们会收到警告 没有这样的模块 'FacebookCore'

import FBSDKCoreKit  
        
        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
                guard let url = URLContexts.first?.url else {
                    return
                }
                ApplicationDelegate.shared.application(
                    UIApplication.shared,
                    open: url,
                    sourceApplication: nil,
                    annotation: [UIApplication.OpenURLOptionsKey.annotation]
                )
            }

ViewController

中添加了以下代码

根据 Facebook 文档,我们必须在 ViewController 中 import FacebookLogin 当我们导入它时,我们会收到警告 No such module 'FacebookLogin'

import FBSDKLoginKit
        
        class ViewController: UIViewController, LoginButtonDelegate
                override func viewDidLoad() {
                    super.viewDidLoad()
                    // Do any additional setup after loading the view.
                    if let token = AccessToken.current,
                            !token.isExpired {
                        let token = token.tokenString
                        let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
                        request.start { connecton, result, error in
                            print("\(result)")
                        }
                    } else{
                        let loginButton = FBLoginButton()
                                loginButton.center = view.center
                        loginButton.delegate = self
                        loginButton.permissions = ["public_profile", "email"]
                                view.addSubview(loginButton)
                    }
                }
            
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
    let token = result?.token?.tokenString
    let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
    request.start { connecton, result, error in
        print("\(result)")
    }
}

我已经尝试了 this but I am getting this error while setting development environment.

中的第 2 步

我认为您弄错了 Facebook 登录步骤。

第 1 步:- 安装以下 pods

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

第 2 步:- 在 info.plist 文件

中添加必要的 Facebook 凭据(Facebook 应用程序 ID)和 CFBundleURLTypes

第 3 步:- 在您想要 Facebook 登录功能的 UIViewController 中导入 FacebookCoreFacebookLogin

调用此方法登录facebook

func setupFacebookLogin(){
    let loginManager = LoginManager()
    loginManager.logOut()
    loginManager.logIn(permissions: ["public_profile", "email"], from: self, handler: { result, error in
        if error != nil {
            print("ERROR: Trying to get login results")
        } else if result?.isCancelled != nil {
            print("The token is \(result?.token?.tokenString ?? "")")
            if result?.token?.tokenString != nil {
                print("Logged in")
                let graphRequest: GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id, first_name, middle_name, last_name, name, picture, email, gender, birthday"])
                graphRequest.start {  _, result, error in
                    if error != nil {
                        let data: [String: AnyObject] = result as! [String: AnyObject]
                        print("Facebook user data - ",data)
                }
            } else {
                print("Cancelled")
            }
        }
    })
}