Home > Blockchain >  How to convert Google iOS Sign-In single page sample AppDelegate.h protocol -> to a segue to Logi
How to convert Google iOS Sign-In single page sample AppDelegate.h protocol -> to a segue to Logi

Time:09-23

Google's Sign-In sample on GitHub Segue to Login Page

So my question is what is the AppDelegate.swift Google Sign-In code for the fields shown below to display the basic profile info:

    // Show the app's signed-out state.
  } else {
    // Show the app's signed-in state.

CodePudding user response:

I may not able to understand your problem clearly. But I am trying to answer based on my understanding.

You can create a class (GoogleLoginManager) for all google login related stuff and create a button in UI then call this method (signIn) from button action.

@IBAction func googleButtonAction(_ sender: Any) {
    
    GoogleLoginManager.shared.signIn(controller: self) { (profile) in
        print("GoogleLogin profile : \(String(describing: profile.name)), \(String(describing: profile.email))")
        
    } onFailure: { (error) in
        print("GoogleLogin error : \(String(describing: error.localizedDescription))")
    }
}

import Foundation import GoogleSignIn

class GoogleLoginManager: SocialLogin {

fileprivate var onSuccess : success?
fileprivate var onFailure : failure?

static let shared = GoogleLoginManager()
private override init() { }

func signIn(controller: UIViewController, onSuccess : @escaping success, onFailure : @escaping failure) {
    
    self.onSuccess = onSuccess
    self.onFailure = onFailure
    
    GIDSignIn.sharedInstance().clientID = GOOGLE_CLIENT_ID
    GIDSignIn.sharedInstance().delegate = self
    
    GIDSignIn.sharedInstance().presentingViewController = controller
    GIDSignIn.sharedInstance().signIn()
    
    // Automatically sign in the user.
    // GIDSignIn.sharedInstance()?.restorePreviousSignIn()
}

func signOut() {
    GIDSignIn.sharedInstance().signOut()
}

}

extension GoogleLoginManager : GIDSignInDelegate {

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
            print("The user has not signed in before or they have since signed out.")
        }
        else if (error as NSError).code == GIDSignInErrorCode.canceled.rawValue {
            print("user canceled the sign in request")
        }
        else {
            print("\(error.localizedDescription)")
        }
        self.onFailure?(error)
        return
    }
    var profile =  SocialProfileModel.init(user: user)
    profile.loginSuccess = true
    self.onSuccess?(profile)
}

func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
          withError error: Error!) {
    // Perform any operations when the user disconnects from app here.
    print("GIDSignIn : didDisconnectWith")
    
}

}

CodePudding user response:

I just had to modify my above AppDelegate.swift slightly - adding a standard UIbutton linked to the following action - gets the profile info:

 @IBAction func LogInButtonTouched(_ sender: UIButton) {
  GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
      guard error == nil else { return }
      guard let user = user else { return }

      let emailAddress = user.profile?.email

      let fullName = user.profile?.name
      let givenName = user.profile?.givenName
      let familyName = user.profile?.familyName

      let profilePicUrl = user.profile?.imageURL(withDimension: 320)
    print("GoogleLogin profile : \(String(describing: user.profile?.name)), \(String(describing: user.profile?.email))")
  }
}
  • Related