2016-02-04 73 views
3

我正在使用此tutorial实施通过谷歌应用程序登录。Google登录后不允许重定向到应用程序

  1. 我通过cocoapods安装了GoogleSignIn。 pod 'GoogleSignIn', '~> 2.4.0'
  2. 添加了GSignIn-Bridging-Header.h#import <GoogleSignIn/GoogleSignIn.h>里面。
  3. 创建URL类型:enter image description here

  4. GIDSignInButton类创建一个视图

  5. 添加代码:

    class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate { 
    override func viewDidLoad() { 
        super.viewDidLoad() 
        GIDSignIn.sharedInstance().delegate = self 
        GIDSignIn.sharedInstance().uiDelegate = self 
        GIDSignIn.sharedInstance().clientID = "KEY" 
    } 
    
    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { 
    if let err = error { 
        print(error) 
    } 
    else { 
        print(GIDSignIn.sharedInstance().currentUser.profile.name) 
        print(GIDSignIn.sharedInstance().currentUser.profile.email) 
        self.performSegueWithIdentifier("idSegueContent", sender: self) 
        } 
    } 
    
    
    func signIn(signIn: GIDSignIn!, didDisconnectWithUser user: GIDGoogleUser!, withError error: NSError!) { 
    
    } 
    } 
    
  6. 但是当我点击允许

enter image description here

它将我重定向到google.com,而不是应用程序。

+0

https://developers.google.com/identity/sign-in/ios/start ?ver = swift 使用此 –

+0

嗨,你有任何错误? – Birendra

+0

在控制台中没有错误 – Arti

回答

2

在AppDelegate中,您需要添加的

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool 
{ 
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 
} 

谷歌通过URL发回信息化的实施,上面的方法抓住了这个信息,并发送回应用。

+1

你能告诉我FBSDKApplicationDelegate是如何重定向谷歌登录,当我们用Facebook登录实施时它是错误的作为用户也接受了你的回答。 – karan

+0

FBSDK在登录完成后重定向回你的应用程序,然后应该调用sourceApplication委托方法,然后你应该检查e url.scheme并返回true或false。 –

+0

did not了解它如何工作?你能详细说明吗? – Simmy

2

我终于在一天的工作后解决了这个问题。 @ayman Ibrahim基本上是对的,但我认为答案有点宽泛。 Swift/Objective-C和Google SDK的不同版本在AppDelegate中调用不同版本的canOpenURL方法。我对此并不是100%确定的,并希望得到确认或更正。这是为我工作的那种方法的版本。我使用谷歌的SDK V4.0起,瞄准的iOS 9.1,并使用雨燕3.0:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    let checkFB = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) 
    let checkGoogle = GIDSignIn.sharedInstance().handle(url as URL!,sourceApplication: sourceApplication,annotation: annotation) 
    return checkGoogle || checkFB 
} 

我也是用的FBSDK,所以忽略该行。

+0

另外,出于调试目的,可以尽可能多地定义该方法的版本,并使用断点或打印来确定哪一个被调用。 – dylanthelion

+0

它工作完美,谢谢:) –

0

我确实找到了另一个答案。就我而言,在AppDelegate中我没有实现2种委托方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

首先是委托方法,不应该实行了,它是由第二种方法所取代。但不知何故,我还是同时使用,只是添加

return [[GIDSignIn sharedInstance] handleURL:url 
            sourceApplication:sourceApplication 
              annotation:annotation]; 

在第二个,然后再以某种方式,它只是回调第一种方法则无法浏览到我的应用程序了。

所以我要发布我的情况的人谁可以得到这个错误,我花了太长时间,找出

相关问题