Recommand · May 21, 2021 0

Trying to implement Google Sign between framework and App in Swift?

I have gone all the stack overflow answer related Google sign solutions but none of them helped.

I am creating a framework which has google sign in. I can successfully sign in but the problem is that after completing sign in the delegate method is not getting called. I have tried many solution but the delegate method is not working. Here through the following way I have implemented:

Framework Implementation

public class Framework {

   public init() {}

   public class func initialization(clientId: String) {
       GIDSignIn.sharedInstance()?.clientID = clientId
   }

   public func loadView(in container: UIView) {
       let bundle = Bundle(identifier: "com.framework")
       let storyboard = UIStoryboard(name: "Frameworks", bundle: bundle)
       let vc = storyboard.instantiateViewController(identifier: "ViewControllerID") as ViewController
       vc.view.frame = CGRect(x: 0, y: 0, width: container.bounds.width, height: container.bounds.height)
       container.addSubview(vc.view)
   }
}
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        GIDSignIn.sharedInstance().presentingViewController = UIApplication.shared.windows.first?.rootViewController!
        GIDSignIn.sharedInstance().delegate = self
    }
    
    @IBAction func onGoogleSignInButtonTapped(_ sender: Any) {
        print("onGoogleSignInButtonTapped")
        GIDSignIn.sharedInstance().signIn()
    }
 
}

extension ViewController: GIDSignInDelegate {
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {

            // perform your log in functions here...
            print(user.profile.email!)

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

Sample App Usage

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
           Framework.initialization(clientId: "APP_CLIENT_ID")
           return true
       }
    }
class ViewController: UIViewController {

   override func viewDidLoad() {
       super.viewDidLoad()
            
       let framework = Framework()
       framework.loadView(on: containerView)
   }
}