我正在尝试链接两个firebase帐户,通常用户将使用社交媒体或电子邮件或anonmus帐户登录,然后用户将使用电话登录。我需要链接这两个帐户。
父视图
将调用父视图中的某个功能以注册电话号码
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)
}
}
子视图中的(signUpWithPhoneView)我提出了FUIPhoneAuth
phoneProvider.signIn(withPresenting: self)
子视图
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)
}
}
}
当用户登录时,子视图将自动被解除,并且我具有将在父视图中调用的didSignInWith
函数。我需要关联以前的用户帐户和用户电话帐户
父视图
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'
任何帮助将不胜感激 谢谢
答案 0 :(得分:0)
您正在使用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"
)
// ...