Swift project not segue-ing properly after Facebook login

孤街浪徒 提交于 2019-12-22 10:34:56

问题


The initial ViewController, LoginViewController.swift:

import UIKit

class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.

        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // User is already logged in, do work such as go to next view controller.
            performSegueWithIdentifier("loginSegue", sender: nil)
            print("segued due to login")
        }
        else
        {
            let loginView : FBSDKLoginButton = FBSDKLoginButton()
            self.view.addSubview(loginView)
            loginView.center = self.view.center
            loginView.readPermissions = ["public_profile", "user_friends"]
            loginView.delegate = self
        }
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        print("User Logged In")
        if ((error) != nil)
        {
            // Process error
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            performSegueWithIdentifier("loginSegue", sender: nil)
            /*
            if result.grantedPermissions.contains("email")
            {
                // Do work
            }
            */
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        print("User Logged Out")
    }

}

"segued due to login" is printed to the terminal upon starting up the app every time, so the if-statement is clearly being reached and also the performSegueWithIdentifier() line. However, the segue is not actually performed as the LoginViewController stays on the screen and the next ViewController is not displayed. I have also tried adding the line:

    performSegueWithIdentifier("loginSegue", sender: nil)

in several other locations I know the program is reaching, like right after super.viewDidLoad(). So, the problem seems to be specific to the segue and the problem does not seem to be with Facebook's login.

I have included a screenshot of the storyboard with the segue's attributes:

I can include any other files if needed. I also suspect it could be a similar type bug as this stackoverflow problem. I have tried deleting the placeholders in my UITextViews in all of my ViewControllers, but this did not solve the problem.


回答1:


Ok so here's how your application:didFinishLaunching:withOptions should look like.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    var initialViewController: UIViewController

    if(FBSDKAccessToken.currentAccessToken() != nil){
        let vc = mainStoryboard.instantiateViewControllerWithIdentifier("someOtherViewController") as! SomeOtherViewController
        initialViewController = vc
    }else{
        initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginViewController")
    }

    self.window?.rootViewController = initialViewController

    self.window?.makeKeyAndVisible()

    return true
}


来源:https://stackoverflow.com/questions/33491397/swift-project-not-segue-ing-properly-after-facebook-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!