我已经尝试让“signinwithphonenumber”工作好几天了,但我似乎无法在 iOS 上做到这一点。它在 Android 上工作正常,但尽我所能,我无法让 iOS 版本工作。
这就是我登录 firebase 的方式:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Firebase
await Firebase.initializeApp(
name: '<app name>',
options: DefaultFirebaseOptions.currentPlatform,
).then((_) {
print("Firebase initialized successfully.");
}).catchError((error) {
print("Error initializing Firebase: $error");
});
// Clear Firestore persistence here
try {
await FirebaseFirestore.instance.clearPersistence();
print('Firestore persistence cleared.');
} catch (e) {
print('Error clearing Firestore persistence: $e');
}
// Set Firestore settings
try {
FirebaseFirestore.instance.settings = const Settings(
persistenceEnabled: false,
sslEnabled: false,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
);
print('Firestore settings updated.');
} catch (e) {
print('Error setting up Firestore: $e');
}
globals.fbdb = FirebaseFirestore.instance;
globals.fbauth = FirebaseAuth.instance;
globals.fbs = FirebaseStorage.instance;
// Run the app
runApp(const <appname>());
}
这是我尝试验证电话号码的地方:
static Future<void> verifyPhoneNumber(String phoneNumber) async {
await FirebaseMessaging.instance.getToken();
print("+++ phoneNumber = ${phoneNumber}");
await globals.fbauth.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
// Auto-retrieve verification code
print("Verification Complete has been triggered for some reason!");
},
verificationFailed: (FirebaseAuthException e) {
// Verification failed
print('+++ Verification failed. Error = ${e}');
},
codeSent: (String verificationId, int? resendToken) async {
// Save the verification ID for future use
print('got through to codeSent section');
},
codeAutoRetrievalTimeout: (String verificationId) {},
timeout: Duration(seconds: 60),
);
}
}
这总是会触发“verificationFailed”部分,并显示错误“令牌不匹配”。
这是AppDelegate.swift:
import UIKit
import Firebase
import Flutter
import FirebaseMessaging
import FirebaseAuth
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Firebase initialization
GeneratedPluginRegistrant.register(with: self)
Messaging.messaging().delegate = self
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(userInfo) {
print(userInfo)
return
}
completionHandler(.newData)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")
}
}
Flutter版本=3.24.3<>稳定通道 飞镖3.5.3 开发工具 2.37.3
Xcode:版本 15.4 (15F31d)
Android Studio Koala 功能下降 | 2024.1.2
Mac mini M2 Pro - 32GB
在 iPhone 13(真实设备)上测试
这是我花了几个小时在 StackOverflow 上搜索、阅读并重新阅读 Google 的文档,以及尽我所能想到的一切来让它发挥作用的结果……
...但所有相同的代码(完全)在 Android 上都能完美运行。
帮助!我快失去活下去的勇气了!
经过一周的寻找终于找到了解决方案:
找到文件
firebase_sdk_version.rb
并将版本修改为11.2.0
。运行:
pod update FirebaseAuth Firebase
然后:
pod install
之后,运行您的应用程序,一切都应该正常!