0

I'm working on my first app with a facebook login, and am unable to get a segue to trigger once I'm logged in. The facebook login triggers just fine and logs me in, then I'm dumped back to the login VC.

Here's where I'm triggering the segue:

func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) {

    switch result {
    case .failed(let error):
        print(error)
        break

    case .success(_,_,_):
        print("login succeeded!")
        self.performSegue(withIdentifier: "SearchVC", sender: self.user)

    case .cancelled:
        print("Canceled!")
    }
}

Here are my other FB Login methods:

func facebookButtonClicked(sender: UIButton) {

    let loginManager = LoginManager()

    loginManager.logIn(readPermissions: [.publicProfile], viewController: self) { loginResult in

        switch loginResult {
        case .failed(let error):
            print(error)

        case .cancelled:
            print("User cancelled login")

        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("Logged in")
            self.getFBUserData()
        }
    }
}

func getFBUserData() {

    if FBSDKAccessToken.current() != nil {

        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"]).start(completionHandler: { (connection, result, error) -> Void in

            if error == nil {
                self.userData = result as! [String : AnyObject]
                print(result!)
                print(self.userData)
            }
        })
    }
}

My Segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "SearchVC", let searchVC = segue.destination as? SearchVC { 
        searchVC.user = sender as? User ?? User()
    }
}

In my App Delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func applicationWillResignActive(_ application: UIApplication) {
    FBSDKAppEvents.activateApp()
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
    return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}

func applicationDidBecomeActive(application: UIApplication) {
    // Call the 'activate' method to log an app event for use
    // in FB analytics and advertising reporting.
    AppEventsLogger.activate(application)
    // ...
}

The segue is wired in IB with an identifier of "SearchVC"

Edit: I initially tried performing the segue in the completion block of FBSDKGraphRequest, but it doesn’t trigger there either

I’m not asking how to perform a segue as the suggested duplicate question indicates

maxwell
  • 3,788
  • 6
  • 26
  • 40
froggomad
  • 1,747
  • 2
  • 17
  • 40
  • Possible duplicate of [IOS - How to segue programmatically using swift](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – Jayesh Thanki Feb 06 '18 at 14:38

2 Answers2

3

You should call performSegue method in FBSDKGraphRequest completion.

  func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
      FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
          self.userData = result as! [String : AnyObject]
          print(result!)
          print(self.userData)
          self.performSegue(withIdentifier: "SearchVC", sender: self)
        }
      })
    }
  }
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
0

The project was bugged. I had another segue that wasn't executing code as well as a few other miscellaneous blocks here and there. Deleting my LoginVC file and recreating it solved all issues

froggomad
  • 1,747
  • 2
  • 17
  • 40