spritekit中的Facebook登录始终返回已取消

时间:2019-03-27 14:19:06

标签: ios swift xcode facebook sprite-kit

我目前正在使用以下代码通过自定义的精灵节点按钮登录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返回为yes,而不是登录。

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

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>

有人可以帮助我吗?任何想法都将不胜感激,因为我已经在这里待了一天,但我想不通。

1 个答案:

答案 0 :(得分:1)

您正在检查if result?.isCancelled是否为nil,因此,如果有结果,则该结果将始终为true或false,从不为null。请尝试以下代码(请注意,此代码未经测试,因此,如果我弄乱了语法,请使用正确的代码随意编辑答案)

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()
          }
     })
}