google signin not calling delegate method after success

后端 未结 5 1137
有刺的猬
有刺的猬 2021-01-18 20:06

i am using google sdk for google signin but after entering the email and password

in viewDidLoad i have added delegate

//set Google sign In delegates

相关标签:
5条回答
  • 2021-01-18 20:30

    Add following code in your AppDelegate.m file

    -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    
    
    
        return [[GIDSignIn sharedInstance] handleURL:url
                                   sourceApplication:sourceApplication
                                          annotation:annotation];
        }
    
    0 讨论(0)
  • 2021-01-18 20:33

    The method that get's called is called on GIDSignInUiDelegate, while the method that is not called is called on the GIDSignInDelegate after login. Here's what the GIDSignIn header file says:

    @protocol GIDSignInDelegate <NSObject>        
    - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error;
    
    @protocol GIDSignInUIDelegate <NSObject>
    @optional
    - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error;
    

    Since you have set both the uiDelegate and the delegate to your UIViewController instance, both methods should get called.

    When I encountered the same problem, my mistake was, that I also set the AppDelegate to be the GIDSignInDelegate causing confusion for hours! So my advice: double check if your ViewController still is the delegate of GIDSignIn.sharedInstance when returning from sign in. E.g. set a breakpoint in the callback that get's called:

    - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
        if (GIDSignIn.delegate != self) {NSLog("gotcha!")}
    }
    
    0 讨论(0)
  • 2021-01-18 20:37

    I was having a very similar issue, until realizing that the GIDSignInDelegate was being garbage collected between starting the signin and getting the callback. Just storing a reference to it elsewhere resolved my issues. (presumably Google uses a weak reference)

    0 讨论(0)
  • 2021-01-18 20:46

    AFAIK GIDSignIn class has two delegate properties:

    • delegate
    • uiDelegate

    The method that is not being called for you is defined in uiDeleagte's protocol.

    0 讨论(0)
  • 2021-01-18 20:54

    I had the same problem. I had tried sign in like below.

    // GDrive.swift
    
    import GoogleSignIn
    import GoogleAPIClientForREST
    
    class GDrive: NSObject,GIDSignInDelegate,GIDSignInUIDelegate
    
    func signIn() {
            GIDSignIn.sharedInstance().uiDelegate = self
            GIDSignIn.sharedInstance().delegate = self
            GIDSignIn.sharedInstance().scopes = scopes
            GIDSignIn.sharedInstance().signIn()
        }
    
    func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
            print("Dispatch ")
        }
    
        func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!)
        {
            print("Present View Controller")
        }
    
        func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!)
        {
            print("Dismiss View Controller")
        }
    
        public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)
        {
            print("Sign In")
        }
    }
    
    
    //ViewController.swift
    
    class MainVC: UIViewController {
    
        func signInGDrive() {
              let gClient:GDrive = GDrive() // this line has weak reference
              gClient.signIn()
    
        }
    
    }
    
    

    After moving gClient variable on top of the file delegate functions has been called. Final working code :

    //ViewController.swift
    
    class MainVC: UIViewController {
        let gClient:GDrive = GDrive() 
        func signInGDrive() {
    
              gClient.signIn()
    
        }
    
    }
    

    Thanks @akroy.

    0 讨论(0)
提交回复
热议问题