Guest User

Untitled

a guest
Jun 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import Foundation
  2.  
  3. import GoogleSignIn
  4.  
  5. class GoogleLoginManager: NSObject, GIDSignInUIDelegate, GIDSignInDelegate {
  6.  
  7. static let sharedInstance = GoogleLoginManager()
  8. private override init() {}
  9.  
  10. var viewController: UIViewController?
  11. var completionHandler: ((_ user: UserModel?, _ error:String?) -> Void)?
  12.  
  13. func signIn(controller:UIViewController, completion: @escaping (_ user: UserModel?, _ error:String?) -> Void) -> Void {
  14.  
  15. self.viewController = controller
  16. self.completionHandler = completion
  17.  
  18. GIDSignIn.sharedInstance().delegate = self
  19. GIDSignIn.sharedInstance().uiDelegate = self
  20. GIDSignIn.sharedInstance().signIn()
  21. }
  22.  
  23. func logOutUser() -> Void {
  24. GIDSignIn.sharedInstance().signOut()
  25. }
  26.  
  27. //MARK:Google SignIn Delegate
  28.  
  29. func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
  30. }
  31.  
  32. func sign(_ signIn: GIDSignIn!,
  33. present viewController: UIViewController!) {
  34.  
  35. self.viewController?.present(viewController, animated: true, completion: nil)
  36. }
  37.  
  38. func sign(_ signIn: GIDSignIn!,
  39. dismiss viewController: UIViewController!) {
  40. self.viewController?.dismiss(animated: true, completion: nil)
  41. }
  42.  
  43. func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
  44.  
  45. if (error == nil) {
  46.  
  47. var userProfile:[String:String] = [:]
  48.  
  49. let idToken = user.authentication.idToken // Safe to send to the server
  50.  
  51. userProfile[UserModel.Keys.firstName] = user.profile.name
  52. userProfile[UserModel.Keys.userId] = user.userID
  53. userProfile[UserModel.Keys.email] = user.profile.email
  54. userProfile[UserModel.Keys.userType] = "g"
  55.  
  56. if user.profile.hasImage {
  57.  
  58. let imageURL = user.profile.imageURL(withDimension: 100)
  59.  
  60. if let string = imageURL {
  61. userProfile[UserModel.Keys.picture] = string.absoluteString
  62. }
  63. }
  64.  
  65. let userModel = UserModel(dictionary: userProfile)
  66.  
  67. self.completionHandler!(userModel, nil)
  68.  
  69. }
  70. else {
  71. print("error in sign in")
  72. self.completionHandler!(nil, error.localizedDescription)
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment