在允许后,Google登录不会重定向到应用程序

Art*_*rti 3 google-authentication ios google-plus swift

我正在使用本教程在应用中使用谷歌登录.

  1. 我通过cocoapods安装了GoogleSignIn. pod 'GoogleSignIn', '~> 2.4.0'
  2. 新增GSignIn-Bridging-Header.h#import <GoogleSignIn/GoogleSignIn.h>内.
  3. 创建网址类型: 在此输入图像描述

  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!) {
    
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. 但是当我点击允许时

在此输入图像描述

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

dyl*_*ion 5

经过一天的工作,我终于解决了这个问题.@ayman Ibrahim基本上是对,但我认为答案有点宽泛.不同版本的Swift/Objective-C和Google SDK,在AppDelegate中调用不同版本的canOpenURL方法.我不是100%肯定这一点,并希望得到确认或纠正.这是适合我的那种方法的版本.我正在使用谷歌的SDK v.4.0,目标是iOS 9.1,并使用Swift 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
}
Run Code Online (Sandbox Code Playgroud)

我也在使用FBSDK,所以忽略那一行.