How to programmatically launch into another viewController if your user is logged in through Facebook

后端 未结 2 2128
傲寒
傲寒 2020-12-22 08:39

Basically I have one class called facebookLogin.swift, the login page, and ViewController.Swift the homepage. I have correctly coded all the Facebook login stuff and it all

相关标签:
2条回答
  • 2020-12-22 09:01

    Swift 3 code

    // Check Last AccesToken

        if let accessToken = AccessToken.current {
            print("Last AccesToken -\(accessToken)")
    
            // Redirect to next screen
            self.performSegue(withIdentifier:"NextScreen", sender: self)
        }
        else {
    
            let loginManager = LoginManager()
            loginManager.loginBehavior = LoginBehavior.web
            loginManager.logIn([ .publicProfile , .email, .userFriends], viewController: self) { loginResult in
                switch loginResult {
    
                case .failed(let error):
                    print(error)
                    break
                case .cancelled:
                    print("User cancelled login.")
                    break
                case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                    print("Logged in! \(grantedPermissions) \(accessToken) \(declinedPermissions)")
    
                    // Save Current UserInfo
                    let params = ["fields":"id,name,email,picture"]
                    let graphRequest = GraphRequest(graphPath: "me", parameters: params)
                    graphRequest.start { (urlResponse, requestResult) in
                        switch requestResult {
                        case .failed(let error):
                            print(error)
                        case .success(let graphResponse):
                            if let responseDictionary = graphResponse.dictionaryValue {
    
                                print("\(String(describing: graphResponse.dictionaryValue))")
    
                                UserDefaults.standard.set(responseDictionary, forKey: "userInfo")
                            }
                        }
                    }
    
                    self.performSegue(withIdentifier: "NextScreen", sender: self)
                    break
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-22 09:15

    Your code seems ok. You should consider checking Storyboard ID of your ViewController under Identity section in Storyboard design screen. You should give Storyboard ID as "ViewController". Login screen's Storyboard ID should be "facebookLogin" according to your code.

    0 讨论(0)
提交回复
热议问题