In this blog, we will be looking into how to set up Google Login in your iOS Project i.e in Swift 5. Okay, Now this blog must be too-lengthy ><, bear with me this is worth reading.
Initially, Create a new Project
Create a new project and I have removed the storyboard and made the LoginVC.xib as my root navigation which I have configured in SceneDelegate.swift.
To get more details on .xib as your root navigation check out this blog.
Creating new project in Firebase.
Create a new Project in Firebase.
Select the Default Account For Firebase and click Create Project.
Linking your App with Firebase
Copy this Bundle ID in your clipboard
Click on the iOS+ icon to get started
Paste the BundleID in Apple BundleID which you might have copied in your clipboard.
Then click on Register App
Download this GoogleService-Info.plist and add this file to your Project
After Dragging to the Project a dialog will pop up asking to add to target. Check the Add to Target and click on Finish.
Click Next and head to Step 5 and Click Continue to console.
Adding Packages in your Swift File
Now navigate to File > Add Packages and add https://github.com/firebase/firebase-ios-sdk in the Text Field and then Select your Project in the Add to Project Dropdown. Then click Add Package.
Check these 3 options and click Add Package.
Now Close and reopen your project and here you go, your app is now connected to the Firebase.
Now let's start working for Google Login
Firebase Setup for Google Login
Navigate to Authentication and click Get Started.
Now navigate to Sign-in methods and click on Google which is under Additional providers.
Now Enable the Google Login and select the email address which will be under the dropdown and click Save.
Once when done it will look like this.
Now create a pod file and add pod ‘GoogleSignIn’ in your podFile and in the terminal run pod install. After Close the Project and open the .xcWorkspace file
Open GoogleService-Info.plist file and copy the value of REVERSED_CLIENT_ID
Double click on the main Project and click Project which is under Tagets and under Info tab click on Add icon in Url Types.
Now paste the REVERSED_CLIENT_ID in URL Schemes.
Add this function in your AppDelegate.swift File. This function is available only above iOS 9+ so we need to specify that that by adding @available(iOS 9.0, *)
Drag a UIView in the ViewController and Give some beautiful Constraints to the UIView.
In Custom Class give the UIView class of GIDSignInButton. or else you cannot connect IBOutlet with the Interfacebuilder.
Now connect the IBOutlet button to UIView. This GIDSignInButton is provided from GoogleSignIn Framework. You can simply connect that GIDSignInButton to a UIView. This Google Sign In Button will not render in interface builder. It will render on the runtime of the project
Here is the output of the GoogleSignInButton which is rendered on the runtime. We can configure this button too according to our needs. Now, let's start with adding the functionality for this button.
Add Tap Gesture Recognizer to the Google Sign In Button.
Here is the on-tap function which will be performed after clicking the function. Let's Understand by breaking it down into small parts.
1. Gets the client-ID from our .plist which is provided from the Framework
2. Passing the clientID to GIDConfiguration.
3. Using this GoogleSignIn Framework we get access to .signIn method. In which we need to pass the GIDConfiguration. After Signing In, we get User Info and Error through Closure.
4. If we get an Error, we won’t execute the further code.
5. We are checking whether the authentication and idToken are not nil.
6. we pass those accessToken and idToken from authentication to GoogleAuthProvider and we get Credential.
7. We are passing those credentials to FirebaseAuth so to get the currentUser and also to keep logged In.
8. If we get an Error, we won’t execute the further code.
9. We are Navigation to Home Page If our login is done successfully.
After clicking the Login button a dialog box will be prompted to click on continue and add ur email and password.
If the Login is done successfully a Blank Screen will Appear i.e our Home Screen. Now let's configure our SceneDelegate.swift.
Do a little bit change in your SceneDelegate.swift file
Using the FirebaseAuth Framework we are getting the currentUser data. If it's nil we are navigating to Login Screen else we will be navigating to Home Screen.
Giving Life to Home Screen
Drag UIImageView, UILabel and give constraints to the widgets and connect to the IBOutlets.
Updating the user Data in Home Screen
Add these functions in your Home Screen and add the updateData function in your viewDidLoad() method.
1. Checking whether the curentUser is nil or not from the FirebaseAuth Package.
2. Updating the Labels from the currentUser data.
3. For the profile Image getting the data by making an API call and updating the profileImage on the Main Thread.
4. API Calling function.
Sign out Implementation
Last but not the least, add this code to the LogoutTap function. Basically, we are signing out by calling the .signout method from FirebaseAuth Framework and navigating to Login Screen.