Integrate Google Sign In in SwiftUI (iOS)

Nasir Ahmed Momin
2 min readJul 12, 2020

--

Sing in form by Naveed Ahmed

As Uncle Ben says to Peter Parker, “Great power brings, Great responsibility” & this does not apply to SwiftUI.

SwiftUI embraces Great Power but does not embraces Great Responsibility. (yet iOS 13.4.1).

UIKit has served us very well with third party authentication frameworks, Like Google, Facebook, Twitter, but as SwiftUI is new guy in towns, then we must have to have some hacks to bring those things in SwiftUI.

In this (Whatever you may call) I shall walk you through this hack.

In UIKit, we have a GIDSignInButton for this functionality, so to achieve same functionality we have us UIViewRepresentable protocol around it.

As per old school way import GoogleSignIn in AppDelegate with following line,

GIDSignIn.sharedInstance()?.clientId = "your info.plist's client Id"

Now create a new SwiftUI file by name GoogleSignInButton which must conforms to UIViewRepresentable protocol & with its following variables as shown.

As every SwiftUI developer is bound to use following functions, so I am gonna also do.

makeUIView(...), updateUIView(...) & makeCoordinator() -> ... but in my case updateUIView(...) does not have any effect so I am keeping it as empty rather than removing at all.

Now here come the magic spell, makeCoordinator() -> ... requires an instance of type reference so I am bound to use it as subclass of NSObject & which confirms to GIDSignInDelegate protocol.

class Coordinator: NSObject, GIDSignInDelegate { ... }

If you have came across till here, you are very lucky, because all implementation is completed, Now only usage of it remains.

In your SwiftUI file either ContentView or any other file you like, use the following code.

I don't t want to post a working model because I want you to work by yourself (I am bit lazy now). Now Combine all pieces together & hit Compile/Build, If you succeed congratulation, else repeat till you succeed.

You can reach me

--

--

Nasir Ahmed Momin

Talks with Mobile devices in Swift, Kotlin, Dart, & Objective-C