Not fetch Google user when handle sign in with another Google app using GIDSignIn

后端 未结 4 1461
轮回少年
轮回少年 2021-01-18 01:38

I\'m using Google Sign-In for iOS and when using simulator it\'s working fine because no google app is installed and user is fetch, but when using my iPhone 6 device open yo

4条回答
  •  时光说笑
    2021-01-18 02:34

    I'm going to assume that you are using GIDSignin with your own server which requires you to include a serverclientID with your GIDSignin. This would force my app to try to use youtube or google plus to log in rather than open a webview or even a browser. This would return a GIDSigninError=-1 "a potentially recoverable error.." and would not allow the user to log in.

    The way I solved this was by blocking URLs from google or youtube before they were opened by overriding UIApplication's canOpenURL function. I did this by subclassing UIApplication and implementing canOpenURL like this:

    @interface MyApp : UIApplication
    - (BOOL)canOpenURL:(NSURL *)url;
    @end
    
    @implementation MyApp
    - (BOOL)canOpenURL:(NSURL *)url
    {
        if ([[url scheme] hasPrefix:@"com-google-gidconsent"] || [[url scheme] hasPrefix:@"com.google.gppconsent"]) {
            return NO;
        }
        return [super canOpenURL:url];
    }
    @end
    
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, NSStringFromClass([MyApp class]), NSStringFromClass([AppDelegate class]));
        }
    }
    

    Notice that usually there would be a nil after argv but this is where you put your subclass of UIApplication. This is also how you can use your own subclass of AppDelegate.

    The other solution would be to create a category on UIApplication that overrides canOpenURL and use swizzling to call the original implementation within your custom canOpenURL. This is a good article on swizzling: https://blog.newrelic.com/2014/04/16/right-way-to-swizzle/

    I must warn you though, these two solutions are hacks and you have to be really careful about the side-effects doing this can have on your application. I'm not even sure apple would be ok with this.

提交回复
热议问题