Google OAuth2.0回调如何在iOS应用中运行?

时间:2018-03-07 12:45:39

标签: ios oauth-2.0 google-signin

我想知道iOS应用程序在使用GoogleSignIn软件包授权后通过Safari回拨后获取授权许可。

我跟着instructions on google's developer site告诉我将以下代码添加到AppDelegate,我相信它负责处理它。

func application(application: UIApplication,
   openURL url: NSURL, options: [String: AnyObject]) -> Bool {
      return GIDSignIn.sharedInstance().handleURL(url,
         sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
         annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

但是当我为这个方法设置断点时,我可以看到url,它从未被调用过。我甚至试图删除这个方法,它仍然有效!

有人可以向我解释那是什么样的魔法吗?

1 个答案:

答案 0 :(得分:0)

我已通过以下方式在我的某个应用程序中实施了Google登录...

必需的广告

pod 'GoogleSignIn'

用户点按Google登录按钮

我已经创建了自定义按钮。

@IBAction func btnGoogleSignInTapped(_ sender: UIButton) {

    GIDSignIn.sharedInstance().clientID = kGoogleClientID
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signIn()
}

GIDSignInDelegate实施

extension MyVC: GIDSignInDelegate {

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

    if (error == nil) {
        // Perform any operations on signed in user here.
        let userId = user.userID                  // For client-side use only!
        let idToken = user.authentication.idToken // Safe to send to the server
        let fullName = user.profile.name
        let givenName = user.profile.givenName
        let familyName = user.profile.familyName
        let email = user.profile.email

        debugPrint("user id = \(String(describing: userId))")
        debugPrint("idToken = \(String(describing: idToken))")
        debugPrint("fullName = \(String(describing: fullName))")
        debugPrint("givenName = \(String(describing: givenName))")
        debugPrint("familyName = \(String(describing: familyName))")
        debugPrint("email = \(String(describing: email))")

        //sign in successfull......
    } else {
        debugPrint("Google sign in error :\(error.localizedDescription)")
    }
}

//-----------------------------------------------------

func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {

    debugPrint("User did disconnecte with error !!")
}

//-----------------------------------------------------
}

您需要在项目的info.plist文件中设置URL类型...

enter image description here