Swiftui Swizzling Disabled by Default, Phone Auth Not Working

SwiftUI swizzling disabled by default, phone auth not working

Here's how to implement Phone Number Auth using the new SwiftUI 2 life cycle:

  1. Create a Firebase project and set up PhoneNumber Auth

  2. Add your iOS app to the Firebase project, download and add GoogleService-Info.plist to your project

  3. In Xcode, select the application target and enable the following capabilities:

    • Push notifications
    • Background modes > Remote notifications
  4. Create and register an APNS authentication key on the Apple developer portal

  5. Upload the key to Firebase (under Project Settings > Cloud messaging in the Firebase Console)

  6. Add the Firebase project's reversed client ID to your app's URL schemes

  7. In your Info.plist, set FirebaseAppDelegateProxyEnabled to NO

  8. Implement the AppDelegate as follows:

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
print("SwiftUI_2_Lifecycle_PhoneNumber_AuthApp application is starting up. ApplicationDelegate didFinishLaunchingWithOptions.")
return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("\(#function)")
Auth.auth().setAPNSToken(deviceToken, type: .sandbox)
}

func application(_ application: UIApplication, didReceiveRemoteNotification notification: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("\(#function)")
if Auth.auth().canHandleNotification(notification) {
completionHandler(.noData)
return
}
}

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
print("\(#function)")
if Auth.auth().canHandle(url) {
return true
}
return false
}
}

@main
struct SwiftUI_2_Lifecycle_PhoneNumber_AuthApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
print("Received URL: \(url)")
Auth.auth().canHandle(url) // <- just for information purposes
}
}
}
}

For further reading, I suggest these two articles I wrote:

  • Firebase and the new SwiftUI 2 Application Life Cycle
  • The Ultimate Guide to the SwiftUI 2 Application Life Cycle

Firebase Phone auth fails to pick up on remote notifications registration

you have missed to add this method in your AppDelegate

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)
}

FireBase PhoneNumber Verification Error In Swift

you might missed to add this method in your AppDelegate

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)
}


Related Topics



Leave a reply



Submit