在 XCTest 中访问 Firebase Phone Auth Provider Class Xcode
Access Firebase PhoneAuth Provider in XCTest Class Xcode
我正在尝试访问 firebase phone 在 XCTest class 中验证委托 class。
代码:-
func testVerifyCorrectPhoneNumber(){
FirebaseAuth.PhoneAuthProvider.verifyPhoneNumber("verificationPhone", uiDelegate: nil) { verificationID, error in
XCTAssert((verificationID != nil) == false)
}
}
错误:-
Instance member 'verifyPhoneNumber' cannot be used on type 'PhoneAuthProvider'; did you mean to use a value of this type instead?
截图:-
有人可以向我解释如何在 XCTest Class 中访问 firebase phone 验证委托,我已经尝试使用上面的代码但还没有结果。
如有任何帮助,我们将不胜感激。
提前致谢。
创建 NsObject 并访问它。
代码:-
import Foundation
import Combine
import Firebase
class Access_Method_Clss : NSObject, ObservableObject {
override init() {
let settings = FirestoreSettings()
settings.isPersistenceEnabled = true
self.db = Firestore.firestore()
db.settings = settings
}
static let sharedInstance: Access_Method_Clss = {
let instance = Access_Method_Clss()
// in case we need to setup up
return instance
}()
var didChange = PassthroughSubject<Access_Method_Clss, Never>()
func verifyPhone(verificationPhone: String, completion: @escaping (String?, Error?) -> Void = { verificationID, error in }) {
PhoneAuthProvider.provider().verifyPhoneNumber(verificationPhone, uiDelegate: nil) { verificationID, error in
if let error = error {
print("PhoneAuthProvider error: \(error.localizedDescription)")
}
completion(verificationID, error)
}
}
func signIn(id: String, code: String, completion: @escaping (String?, Error?,Bool) -> Void) {
let credential = PhoneAuthProvider.provider().credential(withVerificationID: id, verificationCode: code)
Auth.auth().signIn(with: credential) { authResult, error in
if let error = error {
completion("", error,false)
}
if let result = authResult {
print("Check User id Valid or not = ",result.user.uid)
self.checkProfileNeedToSetupOrNot(userID: result.user.uid) { (response) in
if response == false {
completion(result.user.uid, error,false)
}else{
Analytics.logEvent("verification_completed", parameters: nil)
completion(result.user.uid, error,true)
}
}
}
}
}
}
使用:-
func verifyPhoneNumberAndLogin(OtpEndrdCode:String,verificationID:String completion: @escaping(Bool,Bool) -> Void) {
if OtpEndrdCode != "" {
Access_Method_Clss.signIn(id: verificationID, code: OtpEndrdCode) { newUserID,error,needToStp in
if let error = error {
completion(false,false)
} else {
completion(true, true)
}
}
}else{
completion(false,false)
}
}
我正在尝试访问 firebase phone 在 XCTest class 中验证委托 class。
代码:-
func testVerifyCorrectPhoneNumber(){
FirebaseAuth.PhoneAuthProvider.verifyPhoneNumber("verificationPhone", uiDelegate: nil) { verificationID, error in
XCTAssert((verificationID != nil) == false)
}
}
错误:-
Instance member 'verifyPhoneNumber' cannot be used on type 'PhoneAuthProvider'; did you mean to use a value of this type instead?
截图:-
有人可以向我解释如何在 XCTest Class 中访问 firebase phone 验证委托,我已经尝试使用上面的代码但还没有结果。
如有任何帮助,我们将不胜感激。
提前致谢。
创建 NsObject 并访问它。
代码:-
import Foundation
import Combine
import Firebase
class Access_Method_Clss : NSObject, ObservableObject {
override init() {
let settings = FirestoreSettings()
settings.isPersistenceEnabled = true
self.db = Firestore.firestore()
db.settings = settings
}
static let sharedInstance: Access_Method_Clss = {
let instance = Access_Method_Clss()
// in case we need to setup up
return instance
}()
var didChange = PassthroughSubject<Access_Method_Clss, Never>()
func verifyPhone(verificationPhone: String, completion: @escaping (String?, Error?) -> Void = { verificationID, error in }) {
PhoneAuthProvider.provider().verifyPhoneNumber(verificationPhone, uiDelegate: nil) { verificationID, error in
if let error = error {
print("PhoneAuthProvider error: \(error.localizedDescription)")
}
completion(verificationID, error)
}
}
func signIn(id: String, code: String, completion: @escaping (String?, Error?,Bool) -> Void) {
let credential = PhoneAuthProvider.provider().credential(withVerificationID: id, verificationCode: code)
Auth.auth().signIn(with: credential) { authResult, error in
if let error = error {
completion("", error,false)
}
if let result = authResult {
print("Check User id Valid or not = ",result.user.uid)
self.checkProfileNeedToSetupOrNot(userID: result.user.uid) { (response) in
if response == false {
completion(result.user.uid, error,false)
}else{
Analytics.logEvent("verification_completed", parameters: nil)
completion(result.user.uid, error,true)
}
}
}
}
}
}
使用:-
func verifyPhoneNumberAndLogin(OtpEndrdCode:String,verificationID:String completion: @escaping(Bool,Bool) -> Void) {
if OtpEndrdCode != "" {
Access_Method_Clss.signIn(id: verificationID, code: OtpEndrdCode) { newUserID,error,needToStp in
if let error = error {
completion(false,false)
} else {
completion(true, true)
}
}
}else{
completion(false,false)
}
}