首页 > 解决方案 > spritekit 中的 Facebook 登录总是返回取消

问题描述

我目前正在使用下面的代码使用自定义精灵节点按钮登录 facebook:

func runLoginForFacebook() {

     print("logging in with facebook...")
     print("FACEBOOK CURRENT TOKEN ---->\(FBSDKAccessToken.current())")

     let login = FBSDKLoginManager()
     let viewController = UIApplication.shared.keyWindow?.rootViewController
     login.logIn(withReadPermissions: ["public_profile"], from: viewController, handler: { result, error in

    if error != nil {
        print("Process error \(error!.localizedDescription)")
    } else if result?.isCancelled != nil {
        print("Cancelled \(error?.localizedDescription)")
    } else {
        print("Logged in")
        signIntoFirebase()
    }
  })
}

现在这允许我登录并点击继续按钮,因此屏幕被关闭,但它总是返回 isCanceled 作为是而不是登录。

我在整个网站上搜索了答案,他们都说应用程序委托是问题,这是我的应用程序委托,这似乎很好:

class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

   return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

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

    let handled: Bool = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    // Add any custom logic here.

    return handled
}

下一个解决方案是,我在 facebook 上的开发人员门户网站中的应用程序中没有任何测试用户,我检查过的情况并非如此,我有相同的帐户作为应用程序的测试用户/管理员登录。

此外,我检查了我的 info.plist 文件,一切似乎都很好(见下文。出于隐私原因,我将 XXXX 放入名称和数字中,该数字检查正常,与开发人员门户 ID 相同)

    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb4961923108XXXXX</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>496192XXXXXXXX</string>
<key>FacebookDisplayName</key>
<string>NaXXX</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-share-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

有人能帮我吗?任何想法都将不胜感激,因为我已经在这里工作了一天,但我无法弄清楚。

标签: iosswiftxcodefacebooksprite-kit

解决方案


你正在检查if result?.isCancelled不是 nil,所以如果你有一个结果,它永远是 true 或 false,永远不会是 nil。请改用以下代码(请注意,这未经测试,因此如果我弄乱了语法,请随时使用正确的代码编辑我的答案)

func runLoginForFacebook() {

     print("logging in with facebook...")
     print("FACEBOOK CURRENT TOKEN ---->\(FBSDKAccessToken.current())")

     let login = FBSDKLoginManager()
     let viewController = UIApplication.shared.keyWindow?.rootViewController
     login.logIn(withReadPermissions: ["public_profile"], from: viewController, handler: { result, error in

          guard let result = result else {
              print("No result found")
          }
          if result.isCancelled {
              print("Cancelled \(error?.localizedDescription)")

          } else if let error = error {
              print("Process error \(error.localizedDescription)")
          } else {
              print("Logged in")
              signIntoFirebase()
          }
     })
}

推荐阅读