【问题标题】:How to implement Firebase Microsoft OAuth in iOS Swift?如何在 iOS Swift 中实现 Firebase Microsoft OAuth?
【发布时间】:2019-05-23 05:07:34
【问题描述】:

我对 Swift 和 Firebase 还很陌生,我正在尝试让 Microsoft OAuth 提供程序按此处所述工作:

https://firebase.google.com/docs/auth/ios/microsoft-oauth?authuser=0

我的应用在运行时是稳定的,但是 MSLoginTapped 按钮什么也不做,在调试时我只能看到 provider.getCredentialWith 似乎没有运行,因为我没有收到我的打印语句。

文档中似乎存在多个错误,因此我尽可能地对其进行了修改,并提交了审查文档的请求。

Firebase 文档 sn-p:

provider.getCredentialWithUIDelegate(nil) { credential, error in
    if error != nil {
        // Handle error.
    }
    if credential != nil {
        Auth().signInAndRetrieveData(with: credential) { authResult, error in
            if error != nil {
                // Handle error.
            }
            // User is signed in.
            // IdP data available in authResult.additionalUserInfo.profile.
            // OAuth access token can also be retrieved:
            // credential.accessToken
        }
    }
}

问题 1:provider.getCredentialWithUIDelegate 无法识别。

问题 2:Auth().signInAndRetrieveData 无法识别。

================================================ ===

这是我正在使用的东西(那是行不通的):

class LoginViewController: UIViewController {

    var ref : DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func MSLoginTapped(_ sender: UIButton) {
        let provider = OAuthProvider(providerID: "microsoft.com")
        provider.getCredentialWith(nil) { (credential, error) in
            if error != nil {
                // Handle error.
                print("Failed to retreive credential.")
                return
            }
            if credential != nil {
                Auth.auth().signInAndRetrieveData(with: credential!) { authResult, error in
                    if error != nil {
                        // Handle error.
                    }
                    // User is signed in.
                    // IdP data available in authResult.additionalUserInfo.profile.
                    // OAuth access token can also be retrieved:
                    // credential.accessToken
                }
            } else {
                print("Credential is nil.")
            }
        }
    }
}

【问题讨论】:

    标签: ios swift firebase oauth firebase-authentication


    【解决方案1】:

    遇到了同样的问题。 让 provider = OAuthProvider(providerID: "microsoft.com")

    provider 是一个局部变量,getCredentialWith 是异步的,ARC 将摆脱 provider。使其成为全局范围。

    Firebase does not show up sign page for microsoft.com in Swift / iOS

    【讨论】:

    • 谢谢你,成功了。我已经把这个目标搁置了一段时间,然后才开始测试你的解决方案。
    • 嗯,它几乎成功了......我可以登录一个用户,但我仍然无法获取登录用户的用户 ID。它与我过去一直使用的插入式电子邮件身份验证屏幕配合得很好,但使用 OAuth 时,authResult 总是返回 nil,因此我无法读取用户电子邮件地址等。
    • 在代码下面添加 self.microsoftAuth = Auth.auth() 你不会得到零。您需要添加如下所示。 provider?.getCredentialWith(nil) { credential, error in if error != nil { } if let x = credential { self.microsoftAuth = Auth.auth() self. authMicrosoft?.signIn(with: x) { authResult, error in if error != nil { // 处理错误。 } print(authResult?.additionalUserInfo?.profile) } } else { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多