电话号码验证失败-Flutter(仅iOS)

时间:2020-05-29 18:59:36

标签: ios swift flutter firebase-authentication

我正在通过Flutter的Firebase身份验证实施OTP电话验证。 在Android上,它像超级按钮一样工作,但另一方面,在iOS上,我无法使其工作。这是我得到的错误:

请注意,我正在使用OneSignal,它在Android和iOS上均可正常运行

颤振:电话号码验证失败。码: verifyPhoneNumberError。消息:如果应用程序委托混乱 禁用,需要UIApplicationDelegate接收远程通知 将被转发到FIRAuth的canHandleNotificaton:方法。

我扑扑的医生:

enter image description here

我的CocoaPods:

enter image description here

我在Flutter上的OPT功能:

import 'package:firebase_auth/firebase_auth.dart';

class SMSFunctions {
  /// Sends the code to the specified phone number.
  static Future<void> sendCodeToPhoneNumber(
      String phoneNo, Function onSuccess, Function onFailed) async {
    FirebaseAuth.instance.signOut();

    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential user) {
      print(
          'Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: $user');
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      print(
          'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
      onFailed();
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      verificationId = verificationId;
      print("code sent to " + phoneNo);
      onSuccess(verificationId);
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      verificationId = verificationId;
      print("time out");
      onFailed();
    };

    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phoneNo,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  static Future<bool> confirmSMS(String smsCode, String verificationId) async {
    print(smsCode);
    print(verificationId);
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: smsCode,
    );
    AuthResult authResult;
    try {
      authResult = await FirebaseAuth.instance.signInWithCredential(credential);
      print(authResult.user);
      final FirebaseUser currentUser = authResult.user;
      if (currentUser != null)
        return true;
      else
        return false;
    } catch (e) {
      print(e);
    }
    return false;
  }
}

插件版本:

firebase_auth: ^0.16.1

到目前为止,这些是我的尝试:

  1. 修改了我的AppDelegate.swift,就像在post
  2. 中一样

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth
    Auth.auth().setAPNSToken(deviceToken, type: .prod)

  }

  func application(_ application: UIApplication,
      didReceiveRemoteNotification notification: [AnyHashable : Any],
      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
      completionHandler(.noData)
      return
    }
    // This notification is not auth related, developer should handle it.
  }

  // For iOS 9+
  func application(_ application: UIApplication, open url: URL,
      options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    if Auth.auth().canHandle(url) {
      return true
    }
    // URL not auth related, developer should handle it.
  }

  // For iOS 8-
  func application(_ application: UIApplication,
                  open url: URL,
                  sourceApplication: String?,
                  annotation: Any) -> Bool {
    if Auth.auth().canHandle(url) {
      return true
    }
    // URL not auth related, developer should handle it.
  }

  func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    for urlContext in URLContexts {
        let url = urlContext.url
        Auth.auth().canHandle(url)
    }
    // URL not auth related, developer should handle it.
  }

  func application(_ application: UIApplication,
                  didReceiveRemoteNotification notification: [AnyHashable : Any],
                  fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
    }
    // This notification is not auth related, developer should handle it.
    handleNotification(notification)
  }



}

  1. 尝试更改FirebaseAuth插件版本,例如post
  2. 更改了我的URLSCHEMES,例如post

    我的google.services-info.plist(复制黄色条纹) enter image description here

    我的info.plist URL方案(带有黄色条纹)///请注意第二项是Facebook URL方案 my URLSCHEMES

  3. 更改了我的FirebaseAppDelegateProxyEnabled,就像在此post
  4. 中一样

enter image description here

  1. Google Docs

    之后,配置了我的Firebase APNs身份验证密钥和reCAPTCHA验证

    enter image description here

enter image description here

  1. 在Xcode上配置我的签名和功能

enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了确切的问题,我进行了一些更改,并使用以下代码对其进行了修复。

AppDelegate.swift

import UIKit
import Flutter
import Firebase
import FirebaseAuth
import UserNotifications
import FirebaseInstanceID

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
        FirebaseApp.configure()
        GeneratedPluginRegistrant.register(with: self)
        if #available(iOS 10.0, *) {
          UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        }
        return true
  }
    override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let firebaseAuth = Auth.auth()
        firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)

    }
    override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let firebaseAuth = Auth.auth()
        if (firebaseAuth.canHandleNotification(userInfo)){
            print(userInfo)
            return
        }

    }
}

Info.plist 我在下面添加了行

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

我从下面的链接中获得了参考。 [https://pub.dev/packages/firebase_messaging] [1]