我正在尝试链接两个 firebase 帐户,通常用户会使用社交媒体或电子邮件或匿名帐户登录,然后用户会使用电话登录。我需要链接这两个帐户。
父 View
将调用父 View 中的函数以使用电话号码进行注册
class welcomeView: UIViewController, FUIAuthDelegate {
//stores current user
var previosUser = FIRUser()
override func viewDidLoad() {
super.viewDidLoad()
if let user = Auth.auth().currentUser {
previosUser = user
}else{
Auth.auth().signInAnonymously() { (user, error) in
if error != nil {
//handle error
}
previosUser = user!
}
}
}
func signUpWithPhone(){
let vc = signUpWithPhoneView()
vc.authUI?.delegate = self
vc.auth?.languageCode = "ar"
self.present(vc, animated: true)
}
}
在 subview (signUpWithPhoneView)中,我展示了 FUIPhoneAuth
phoneProvider.signIn(withPresenting: self)
subview
import UIKit
import FirebaseAuthUI
import FirebasePhoneAuthUI
class signUpWithPhoneView: UIViewController {
fileprivate(set) var authUI = FUIAuth.defaultAuthUI()
var didload = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
if !didload { // stops from looping
didload = !didload
guard let authUI = self.authUI else {return}
let phoneProvider = FUIPhoneAuth.init(authUI: authUI)
self.authUI?.providers = [phoneProvider]
>> phoneProvider.signIn(withPresenting: self)
}
}
}
当用户登录时, subview 将自动关闭,我有 didSignInWith
函数,该函数将在父 View 中调用。我需要关联之前的用户账号和用户手机账号
父 View
func authUI(_ authUI: FUIAuth, didSignInWith user: FirebaseAuth.User?, error: Error?) {
if let user = user{
// link the the two accounts
}else{
}
}
我尝试使用链接
let provider = PhoneAuthProvider.provider(auth: authUI.auth!)
let credential = PhoneAuthProvider.credential(provider)
previosUser.link(with: credential, completion: { (user, error) in
if error != nil {
print("this is linking two acounts error : " , error)
}
})
但是凭证有错误
在 ...previosUser.link(with: *credential*, completion: ...
Cannot convert value of type '(String, String) -> PhoneAuthCredential' to expected
argument type 'AuthCredential'
任何帮助将不胜感激 谢谢
您正在使用 authUI.auth
获取提供程序,然后没有使用它来获取凭据,并且您当前正在使用实例方法 credential
的静态引用。
实例方法本身接受两个字符串并返回一个 AuthCredential
,这就是您看到 (String, String) -> AuthCredential
的原因。您必须使用您必须从用户那里收集的验证 ID 和代码来创建凭据。
guard let auth = authUI.auth else { return }
let provider = PhoneAuthProvider.provider(auth: auth)
let credential = provider.credential(
withVerificationID: "XXXXXXXXXXXX",
verificationCode: "XXXXXX"
)
// ...
关于iOS链接两个firebase帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997670/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |