Created
November 19, 2016 08:40
-
-
Save bishalg/e7b4e3842a5615829d92a1de7139cf1c to your computer and use it in GitHub Desktop.
Facebook Login using FB Login Kit for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MBProgressHUD | |
import FacebookCore | |
import FacebookLogin | |
import FBSDKCoreKit | |
@IBAction func facebookAction(_ sender: UIButton) { | |
showHUDWithMessage("Facebook Login") | |
if let accessToken = AccessToken.current { | |
debugPrint("Already Logged in - accessToken", accessToken) | |
self.graphRequestForME({ (result) in }) | |
} else { | |
let loginManager = LoginManager() | |
loginManager.logIn([ .publicProfile, .userFriends, .email ], viewController: self) { [weak self] loginResult in | |
guard let strongSelf = self else { return } | |
mainQueue() { | |
switch loginResult { | |
case .failed(let error): | |
debugPrint(error) | |
strongSelf.progressHUD.showFailureMessage("Login Failed") | |
let result = Result<User>.failure(error) | |
strongSelf.delegate?.facebookLogin(result) | |
case .cancelled: | |
debugPrint("FB Login User cancelled") | |
strongSelf.progressHUD.showFailureMessage("Login cancelled") | |
let error = LoginError.cancelled | |
let result = Result<User>.failure(error) | |
strongSelf.delegate?.facebookLogin(result) | |
case .success(let grantedPermissions, let declinedPermissions, let accessToken): | |
debugPrint("---- FB Permissions ----") | |
debugPrint("FB grantedPermissions", grantedPermissions) | |
debugPrint("FB declinedPermissions", declinedPermissions) | |
strongSelf.user.accessToken = accessToken | |
strongSelf.user.facebookID = accessToken.userId | |
strongSelf.graphRequestForME({ (result) in }) | |
} // switch loginResult | |
} | |
} | |
} | |
} // IBAction | |
func graphRequestForME(_ callback: @escaping (Result<User>) -> Void) { | |
FBSDKGraphRequest(graphPath: "me", | |
parameters: ["fields": "id, name, first_name, last_name, email, picture.width(720).height(720)"]).start(completionHandler: { [weak self] (connection, result, error) in | |
guard let strongSelf = self else { return } | |
mainQueue() { | |
if error == nil { | |
strongSelf.progressHUD.showSucessMessage("Facebook Login Sucess") | |
guard let currentAccessToken = FBSDKAccessToken.current() else { return } | |
strongSelf.user = strongSelf.user.userFromGraphResult(result as! NSDictionary) | |
strongSelf.user.tokenString = currentAccessToken.tokenString | |
let result = Result.success(strongSelf.user) | |
strongSelf.delegate?.facebookLogin(result) | |
} else { | |
strongSelf.progressHUD.showFailureMessage("Facebook Login Failed") | |
delay(1) { | |
let result = Result<User>.failure(error!) | |
strongSelf.delegate?.facebookLogin(result) | |
} | |
} // if error | |
} // mainQueue | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment