Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Login In Login View Controller Instead of Appdelegate IOS Swift

Can I able do google sign in function in login view controller button action.

Now with google sign in sdk we are implementing in appdelegate and using another view controller to view that signin button.

instead of implementing the function in the appdelegate, can we able to implement on the view controller as login view controller and handle the google function in a button action.

I'm new to implementing SDK in ios. If any help will be useful.

like image 822
Sridhar G.K Avatar asked Dec 25 '22 07:12

Sridhar G.K


2 Answers

I have posted the complete solution for the Google + Sign In purpose for iOS 9 and iOS 8 and have also taken care that the Google login flow is not moving outside the app in safari browser which is a very common reason for app rejection from Appstore.

Here is the link
Google + login iOS App rejection from appstore using google sdk v3.x

You just have to implement the GIDSignInDelegate in your own custom view controller rather than doing in AppDelegate. So according to your question, your custom view controller will be LoginViewController.

like image 177
Rajan Maheshwari Avatar answered Dec 28 '22 10:12

Rajan Maheshwari


If you'd like to use a custom view controller for your Google Sign In process using v5.0.2 and Swift 5, perform the following...

Follow the instructions to download and install the GoogleSignIn kit into your app. See Start integrating Google Sign-In into your iOS app.

In your AppDelegate.swift file, do the following:

  1. Add the following to the import portion:

    import GoogleSignIn
    
  2. Add the following lines at the bottom of this function:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        // other code here...
    
        // Initialize Google sign-in
        GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
    
        return true
    }
    
  3. Make sure you REMOVE this line from your AppDelegate.swift file:

    GIDSignIn.sharedInstance().delegate = self // REMOVE THIS LINE!
    

    Also, DO NOT use GIDSignInDelegate in your AppDelegate.swift class header!

  4. Add this function, if it does not already exist and add the following inside:

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }
    

    If your app uses Facebook and Google to sign in, use this:

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        var flag: Bool = false
        if ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
    
            // Handle Facebook URL scheme
            flag = ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
        } else {
    
            // Handle Google URL scheme
            flag = GIDSignIn.sharedInstance().handle(url)
        }
        return flag
    }
    

Your AppDelegate.swift file is done! Next, perform the following on your custom log in view controller, which we will call LogInViewController.swift, like so:

  1. Add the following to the import portion:

    import GoogleSignIn
    
  2. Add the GIDSignInDelegate to your class header like this:

    class LoginViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate, GIDSignInDelegate {
    
        // your class code here...
    
    }
    
  3. In your override func viewDidLoad(), add the following on the bottom:

    GIDSignIn.sharedInstance().delegate = self
    
  4. To conform with GIDSignInDelegate, make sure to add the following functions:

    // Retrieve user profile details from Google
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
    
            // perform your log in functions here...
    
            // ex. assign user profile data to variables
            self.appUser.email = user.profile.email
            self.appUser.firstname = user.profile.givenName
            self.appUser.lastname = user.profile.familyName
    
            // etc.
    
            // Sign out of Google when logic completes for security purposes
            GIDSignIn.sharedInstance().signOut()
    
        } else {
            print(error.localizedDescription)
        }
    }
    

    And also add this function to comply with the delegate:

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print(error.localizedDescription)
        }
    }
    

That's all it should take to create a custom view controller. I did not use GIDSignInDelegate nor its delegate functions in AppDelegate.swift, in fact, I deleted them altogether. The app performs flawlessly. I hope this helps someone!

Just giving back to the StackOverflow Community for all you've done for me.

like image 44
WhiteEagle Avatar answered Dec 28 '22 11:12

WhiteEagle