Ionic iOS应用中@capacitor-firebase/authentication的signInWithPhoneNumber失效排查
手机号认证功能异常排查(Ionic/Angular + Capacitor Firebase)
问题背景
我正在开发一款Ionic/Angular应用,使用@capacitor-firebase/authentication库实现手机号用户认证功能,相关实现代码如下:
手机号登录方法
public async signInWithPhoneNumber(options: SignInWithPhoneNumberOptions, loading: HTMLIonLoadingElement ): Promise<void> { try { console.log("Attempting to sign in with phone number:", options.phoneNumber); const user = await FirebaseAuthentication.signInWithPhoneNumber(options); console.log('Sign-in successful'); } catch (error) { console.error('Error during phone number verification:', error); } finally { await loading.dismiss(); } }
事件监听器配置(服务构造函数)
constructor(private readonly ngZone: NgZone) { FirebaseAuthentication.removeAllListeners().then(() => { FirebaseAuthentication.addListener('phoneCodeSent', async (event) => { this.ngZone.run(() => { console.log("phoneCodeSent"+ event.verificationId); }); }); FirebaseAuthentication.addListener( 'phoneVerificationCompleted', async (event) => { this.ngZone.run(() => { console.log("phoneVerificationCompleted"+ event.user?.uid); }); }, ); FirebaseAuthentication.addListener( 'phoneVerificationFailed', async (event) => { this.ngZone.run(() => { console.log("phoneVerificationFailed"+ event.message); }); }, ); }); }
预期行为
调用FirebaseAuthentication.signInWithPhoneNumber(options)后,Firebase发送短信验证码,方法返回包含用户信息或verificationId的对象,同时触发对应事件。
实际行为
在iOS设备运行时,Xcode控制台日志如下:
⚡️ [log] - Attempting to sign in with phone number: +573218116768 ⚡️ To Native -> FirebaseAuthentication signInWithPhoneNumber 110818811 ⚡️ TO JS undefined ⚡️ [log] - Sign-in successful
原生层返回undefined,无错误抛出,Firebase未发送短信,所有事件监听器无任何输出。
补充信息
Firebase配置
- Firebase控制台已开启手机号认证
- GoogleService-Info.plist已正确配置并同步到项目
环境
- Ionic: 8.x
- Angular: ^18.x
- @capacitor-firebase/authentication: ^6.3.1
- Capacitor: 6.x
已检查项
- Firebase配置正确(未配置APNs,原以为手机号认证无需此项)
- 手机号为国际格式
- Capacitor插件已正确安装并同步
核心问题
- 什么原因导致
signInWithPhoneNumber不触发任何事件且Firebase不发送短信? - 仅使用手机号认证时,iOS是否必须配置APNs证书?
寻求解决指导与调试步骤。
补充编辑信息
App Delegate代码
import UIKit import Capacitor import FirebaseCore @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FirebaseApp.configure() return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { // Called when the app was launched with a url. Feel free to add additional processing here, // but if you want the App API to support tracking app url opens, make sure to keep this call return ApplicationDelegateProxy.shared.application(app, open: url, options: options) } func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { // Called when the app was launched with an activity, including Universal Links. // Feel free to add additional processing here, but if you want the App API to support // tracking app url opens, make sure to keep this call return ApplicationDelegateProxy.shared.application(application, continue: userActivity, restorationHandler: restorationHandler) } }
Podfile代码
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers' platform :ios, '13.0' use_frameworks! # workaround to avoid Xcode caching of Pods that requires # Product -> Clean Build Folder after new Cordova plugins installed # Requires CocoaPods 1.6 or newer install! 'cocoapods', :disable_input_output_paths => true def capacitor_pods pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorFirebaseAuthentication', :path => '../../node_modules/@capacitor-firebase/authentication' pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' pod 'CapacitorCamera', :path => '../../node_modules/@capacitor/camera' pod 'CapacitorHaptics', :path => '../../node_modules/@capacitor/haptics' pod 'CapacitorKeyboard', :path => '../../node_modules/@capacitor/keyboard' pod 'CapacitorPreferences', :path => '../../node_modules/@capacitor/preferences' pod 'CapacitorStatusBar', :path => '../../node_modules/@capacitor/status-bar' end target 'App' do capacitor_pods # Add your Pods here end post_install do |installer| assertDeploymentTarget(installer) end
内容的提问来源于stack exchange,提问作者AlejoDev
相关产品推荐
相关产品推荐

