问题
Im trying to implement the new Google SignIn Library that I installed using Cocoapods. Deployment Target is 8.4 and Im testing in a 9.0.2 device.
1- I have the proper plist file with clientID and reverseID
2- I have the URL types set
3- I have the AppDelegate as a delegate of GIDSignIn [GIDSignIn sharedInstance].delegate = self; and the proper delegate methods implemented to catch the callback - (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error
4- I have the - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{ return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
5- Im using the automatic GIDSignInButton class and is placed in a subclass of UIViewController, so I dont need to implement the GIDSignIn uiDelegate methods. But I still have [[GIDSignIn sharedInstance] setUiDelegate:self]; set in the viewDidLoad
6- I have SFSafariViewController.framework in the Linked Libraries.
7- I never manually set the clientID to GIDSignIn because that is not part of the instructions at google's official documentation. Although many people keep doing it I dont know why.
The Problem: Once I press the Login button, a SFSafariViewController gets presented, I select the account I want to use (notice it never went out of my app into any other google apps for authentication even though I have several installed). The permissions get presented, I see the name & logo of my app that I configured in Google Developer Console, I accept the permissions. The SFSafariViewController falls back to "www.google.com" and thats it. The only way to dismiss this screen is to tap on the "OK" button in the uppper right corner, and then the - (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error gets called, with error saying the used Cancelled the login process.
回答1:
Just solve it. As you are using CocoaPods, seems like you have to set
Swift:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
Obj-c
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
in your appDelegate. Now you will be redirected to your app after accepting the permissions.
More info: https://developers.google.com/identity/sign-in/ios/sign-in
回答2:
I found out the problem. Google Official Documentation mentions the need to implement the following method in your AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
But it never mentions the need to implement also a similar method called
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
I had to implement both methods in order for my app to work properly. I have facebook authentication too, so my methods ended up looking like the following.
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
return [[FBSDKApplicationDelegate sharedInstance] application:app
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]
|| [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsSourceApplicationKey]];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
] ||
[[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
来源:https://stackoverflow.com/questions/33907831/google-signin-sfsafariviewcontroller-webview-redirects-to-google-com-after-accep