How can I do a Facebook login using Swift 2.0 and iOS 9.1?

最后都变了- 提交于 2019-12-03 08:02:17

You're on the right track.

Have you set up your pList yet?

You're gonna have to add to your VC, add a login button, deal with delegate, and then deal with FB info. Something like (but not exactly) this:

class yourVC: UIViewController, FBSDKLoginButtonDelegate, UITextFieldDelegate 

{
var loginView : FBSDKLoginButton = FBSDKLoginButton()

viewDidLoad() {
            loginView.frame = CGRectMake(20, 20, theWidth-40, 40)
            self.view.addSubview(loginView)
            loginView.readPermissions = ["public_profile", "email", "user_friends","user_birthday"]
            loginView.delegate = self

}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if ((error) != nil)
        {
        //handle error
        } else {
                returnUserData()
        }
    }

func returnUserData()
    {


  let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)"])
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            print("Error: \(error)")
        }
        else
        {
            print("fetched user: \(result)")
            let id : NSString = result.valueForKey("id") as! String
            print("User ID is: \(id)")
            //etc...
         }
      })
}

You're gonna have to play around with the returnData() part to get it right, but this code should get you 90% of the way there.

I've included a wide range of permissions (user age, interested in, etc), but you'll have to configure them yourself. Honestly this part (the hard part) is really similar with it's object C counterpart, which is much better documented. Just download some get projects where it's working, and try to parse them together into something that suits your fancy.

It can be hard to get it all working, but give it a go, and keep at it!

If you want to perform login from Facebook on click of your custom button, this will help:

@IBAction func onClickFacebookButton(sender: UIButton){
    let login = FBSDKLoginManager()
    login.loginBehavior = FBSDKLoginBehavior.SystemAccount
    login.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {(result, error) in
        if error != nil {
            print("Error :  \(error.description)")
        }
        else if result.isCancelled {

        }
        else {
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, picture.type(large), email, name, id, gender"]).startWithCompletionHandler({(connection, result, error) -> Void in
                if error != nil{
                    print("Error : \(error.description)")
                }else{

                    print("userInfo is \(result))")

                }
            })
        }

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