【问题标题】:How does Google OAuth2.0 callback works in iOS app?Google OAuth2.0 回调如何在 iOS 应用中工作?
【发布时间】:2018-08-15 13:53:14
【问题描述】:

我想知道 iOS 应用在使用 GoogleSignIn 包时在获得 Google 授权后从 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,它从来没有被调用过。我什至尝试删除此方法,它仍然有效!

谁能给我解释一下这是什么魔法?

【问题讨论】:

    标签: ios oauth-2.0 google-signin


    【解决方案1】:

    我已通过以下方式在我的一个应用程序中实现了 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 类型...

    【讨论】:

    • 嗯,谢谢,但我的问题是不是,它不起作用。我不明白它是如何工作的。从 Safari 返回后如何调用 sign(_:didSignInFor:withError:) 函数?
    • Info.plist文件下需要设置URL Types....设置app时goolge给出的步骤。
    • 你不明白我的问题。在 AppDelegate 中使用 URL func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) 调用我的应用程序后,应该调用它,对吗?但事实并非如此。它仍然有效。如何?应用程序如何知道它是从 Safari 回调的?
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2015-03-12
    • 2018-08-05
    • 2017-05-23
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    相关资源
    最近更新 更多