0

I am stuck after coding the Facebook login.....!

After the login procedure I just end up with a logout button.

I would like to make use of the App Invites for iOS but I cant figure out how to integrate it... The documentations on "Facebook.developer" is hard to understand.

This is my code so far in the view controller:

import UIKit
import FacebookLogin
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
var dict : [String : AnyObject]!

override func viewDidLoad() {
    super.viewDidLoad()

    //creating button
    let loginButton = LoginButton(readPermissions: [ .publicProfile, .userFriends, .email ])
    loginButton.center = view.center

    //adding it to view
    view.addSubview(loginButton)

    //if the user is already logged in
    if let accessToken = FBSDKAccessToken.current(){
        getFBUserData()
    }
}

//when login button clicked
@objc func loginButtonClicked() {
    let loginManager = LoginManager()
    loginManager.logIn([ .publicProfile, .userFriends, .email ], 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):
            self.getFBUserData()
        }
    }
}

//function is fetching the user data
func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil){
                self.dict = result as! [String : AnyObject]
                print(result!)
                print(self.dict)
            }
        })
    }
}


func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    /*
     Check for successful login and act accordingly.
     Perform your segue to another UIViewController here.
     */
    let viewController = self.storyboard?.instantiateInitialViewController("StoryBoardID") as? UITableViewController
    self.presentViewController(viewController, animated: true, completion: nil)

}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    // Actions for when the user logged out goes here
     }
}

Any help?

  • what is your goal? it shows log out after you log in. It is handled automatically. if you want a custom button check this: https://developers.facebook.com/docs/swift/login – Tung Fam Aug 15 '17 at 17:18
  • Hi My goal is do integrate "App invites "after Login Dialog is completed. The best I could find so far is this link, but it does explain how to make it work with the Login Dialog. https://github.com/facebook/facebook-sdk-swift/blob/master/Samples/Catalog/Sources/AppInviteViewController.swift – Frederik Haahr Aug 16 '17 at 08:51
  • make the login and then make app invite. good login tutorial is here: https://www.youtube.com/watch?v=MNfrBdyEvmY – Tung Fam Aug 16 '17 at 10:05

0 Answers0