Skip to content

Swift Developer Blog

Learn Swift and App Development for iOS

  • Swift Blog
    • UITableView
    • UIImageView
    • UIScrollView
    • UIWebView
    • SubView
    • UIImagePickerController
    • UIPageViewController
    • UITabBarController
    • UIAlertController
    • UISegmentedControl
    • Xcode
    • Firebase
  • Video Tutorials
  • Code Examples
    • Code examples
    • Cheat Sheets
  • Resources
    • Swift Developer
  • Full stack
    • Developer Resources
    • Android & Kotlin
    • Java
    • RESTful Web Services
    • Amazon Web Services
    • Firebase with Kotlin
    • Firebase with Swift
    • Hibernate
    • Git
    • Design Patterns
  • About
  • Contact me
  • Home
  • Cocoa Touch and Swift tutorials
  • Sign-in with Google Account Example in Swift
August 19, 2015 by Sergey Kargopolov 7 comments on "Sign-in with Google Account Example in Swift"

Sign-in with Google Account Example in Swift

Earlier I have published a video tutorial on Sign-in with Facebook account and Sign-in with Twitter account. In this video tutorial I am going to share with you how to implement Sign-in with Google account feature for your mobile app.

I hope you find these two videos valuable.

If you are interested in learning more about User Sing-up for your mobile app process with either Parse or PHP & MySQL, there are more videos I have which I have organized into two videos courses:

1. Sign-in & Sign-up with Swift & Parse video course: https://goo.gl/a4jWz1
2. Sign-in & Sign-up with Swift, PHP and MySQL: https://goo.gl/CG4xKY

and here is now Sign-in with Google Account:

Link to Google Guides on Sign-in with Google Account.

Video 1. Configure Xcode project.


Video 2. Implement Sign-in and Sign out button

 

AppDelegate source code:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        // Initialize sign-in
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        
        GIDSignIn.sharedInstance().delegate = self
        
        return true
    }
    
    func application(application: UIApplication,
        openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
            return GIDSignIn.sharedInstance().handleURL(url,
                sourceApplication: sourceApplication,
                annotation: annotation)
    }
    
    
    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
        withError error: NSError!) {
            if (error == nil) {
                // Perform any operations on signed in user here.
                //let userId = user.userID                  // For client-side use only!
                //let idToken = user.authentication.idToken // Safe to send to the server
                let name = user.profile.name
                //let email = user.profile.email
                
                print("User name \(name)")
                
                
                // [START_EXCLUDE]
                NSNotificationCenter.defaultCenter().postNotificationName(
                    "ToggleAuthUINotification",
                    object: nil,
                    userInfo: ["statusText": "Signed in user:\n\(name)"])
                // [END_EXCLUDE]
                
                let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
                
                let protectedPage = myStoryBoard.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
                
                let protectedPageNav = UINavigationController(rootViewController: protectedPage)
 
                self.window?.rootViewController = protectedPageNav
                
            } else {
                print("\(error.localizedDescription)")
                // [START_EXCLUDE silent]
                NSNotificationCenter.defaultCenter().postNotificationName(
                    "ToggleAuthUINotification", object: nil, userInfo: nil)
                // [END_EXCLUDE]
            }
    }
    
    
    func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
        withError error: NSError!) {
            // Perform any operations when the user disconnects from app here.
            // [START_EXCLUDE]
            NSNotificationCenter.defaultCenter().postNotificationName(
                "ToggleAuthUINotification",
                object: nil,
                userInfo: ["statusText": "User has disconnected."])
            // [END_EXCLUDE]
    }

    func applicationWillResignActive(application: UIApplication) {
         
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
    }

    func applicationDidBecomeActive(application: UIApplication) {
         
    }

    func applicationWillTerminate(application: UIApplication) {
         
    }


}

Download source code to this project from GitHub.

Happy learning!

Sergey

Posted in: Cocoa Touch and Swift tutorials, User login and Registration with Swift
Tagged: Sign in, Sign in with Google

Post navigation

Previous Previous post: UISegmentedControl with UITableView example in Swift. Part 2.
Next Next post: Play music MP3 file example in Swift

Primary Sidebar

Find videos

Unit Testing Swift?

Unit Testing Swift Mobile App

Recent Posts

  • Inject JavaScript into WKWebView
  • WKWebView. Load HTML File from App Bundle.
  • UIImagePickerController in Swift with Firebase. Cheat Sheet.
  • Firebase Realtime Database Cheat Sheet
  • Push Notifications with Firebase Cloud Messaging – Cheat Sheet

Search by Tag

AWS Facebook Facebook SDK Facebook Sign in Firebase HTTP HTTP Get HTTP Post Icon Java Keychain Load Images Login MMDrawerController MySQL Navigation Drawer NSURLSession Parse Password PHP Push Notifications Search Sign in SubView Swift UIActivityIndicator UIAlertController UIImage UIImagePickerController UIImageView UIPageViewController UIScrollView UISearchBar UISegmentedControl UITabBarController UITableView UITableViewCell UITableViewController UITableViewDatasource UIWebView Upload Xcode Xcode Tips Xcode Tips and Tricks XIB

Featured posts

  • Left side menu (Navigation Drawer) example with Swift
  • Creating custom user interface files with XIB in Xcode 6 and Swift
  • Add Subview and Remove subview example in Swift
  • Customize UINavigationBar appearance in Swift
  • MBProgressHUD example in Swift
  • iOS Keychain example in Swift
  • Image Upload with Progress Bar example in Swift
  • Circular Image or Image with Rounded Corners. Example in Swift

Copyright © 2023 Swift Developer Blog. Powered by WordPress and Themelia.

Would you like more video tutorials weekly?
  • Swift programming with Parse. Practical Examples.
  • Swift programming with PHP and MySQL. Practical examples.
  • iOS Mobile Apps Development with Swift

Enter your email and stay on top of things,

Subscribe!