用Swift登录Facebook后,Segue不会触发 [英] Segue won't trigger after Facebook login with Swift

查看:112
本文介绍了用Swift登录Facebook后,Segue不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前感谢您的帮助!

我正在我正在写的一个快速应用程序中实现Facebook登录,并且在执行登录完成。

I am implementing facebook login on a swift app I'm writing, and I am having some trouble performing a segue after the login completes.

作为FBSDKLoginButtonDelegate的一部分,我有两种方法:

As part of FBSDKLoginButtonDelegate, I have two methods:

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result:
 FBSDKLoginManagerLoginResult!, error: NSError!)
    {}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
    {}

登录按钮正确实现,我可以读取权限/ FB令牌,并将其传递到Amazon Cognito。

The login button is implemented correctly, I am able to read permissions/FB tokens, and pass this onto Amazon Cognito.

但是,奇怪的是,我无法以编程方式将其登录到另一个视图(控制器)中。

The strange thing, though, is that I cannot programmatically segue out of the login, into a different view (controller).

按下FB登录按钮将用户带到网络浏览器(Facebook SDK处理所有这些),登录ta kes place。

Pushing the FB Login Button takes the user to a web browser (the Facebook SDK handles all of this), and the login takes place.

如果我放置一个segue,如:

If I place a segue, like:

dispatch_async(dispatch_get_main_queue(), {
        self.performSegueWithIdentifier("segueLoginViewtoHome", sender: nil })

进入loginButton(),它永远不会真正执行(即,当Web浏览器消失时,用户将返回到具有登录按钮的视图,而不是由segue打算的新视图)。我相信在浏览器窗口返回到包含登录按钮(和segue)的视图之前执行该行,但是我不知道如何告诉程序等待视图成为唯一一个活动并再次可见执行segue。

into loginButton(), it never truly executes (i.e, when the web browser goes away, the user is returned to the view with the login button, not the new view intended by the segue). I believe the line executes before the browser window returns to the view housing the login button (and the segue), but I have no clue how to tell the program to "wait" for the view to become the only one active and visible again to perform the segue.

我相信是这样,因为如果我把同样的segue代码放在loginButtonDidLogOut()中,在那里不需要切换到浏览器,在退出按下从屏幕底部向上滑动的工作表中,segue执行正常。

I believe this is the case, because if I were to put the same segue code into loginButtonDidLogOut(), where no switch to a browser is needed, the segue executes just fine after "log out" is pushed in the sheet that slides up from the bottom of the screen.

此视图控制器正确设置为委托,如图所示,如果编码在退出时发生,则该功能正常工作。

This view controller is correctly set as the delegate, as shown by the segue functioning correctly if coded to occur upon logout.

任何帮助将不胜感激,对于长期的问题感到歉意,谢谢!

Any help would be much appreciated, apologies for the long question, and thanks!

推荐答案

我通过创建一个名为fbLoginSuccess的全局布尔值来初始化为false来解决这个问题。如果Facebook登录成功,那么我将fbLoginSuccess设置为true,在那里执行您的segue。然后我覆盖viewDidAppear(),并在fbLoginSuccess为true时执行segue。这样做是因为viewDidAppear()在从浏览器窗口返回后被执行,这是你想要的。

I fixed this problem by creating a global boolean called fbLoginSuccess initialized to false. If the Facebook login was successful then I set fbLoginSuccess to true where you are performing your segue. Then I overrode viewDidAppear() and perform the segue there if fbLoginSuccess is true. This fixes it because viewDidAppear() is executed after returning from browser window, which is what you want.

var fbLoginSuccess = false

override func viewDidAppear(animated: Bool) {
    if (FBSDKAccessToken.currentAccessToken() != nil && fbLoginSuccess == true)
    {
        performSegueWithIdentifier("loginSegue", sender: self)
    }
}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    print("User Logged In")

    if ((error) != nil) {
        // Process error
        print(error)
    }
    else if result.isCancelled {
        // Handle cancellations
    }
    else {
        fbLoginSuccess = true
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("email") {
            // Do work
        }
    }
}

希望有帮助!

这篇关于用Swift登录Facebook后,Segue不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆