SwiftUI swizzling disabled by default, phone auth not working
Here's how to implement Phone Number Auth using the new SwiftUI 2 life cycle:
Create a Firebase project and set up PhoneNumber Auth
Add your iOS app to the Firebase project, download and add
GoogleService-Info.plist
to your projectIn Xcode, select the application target and enable the following capabilities:
- Push notifications
- Background modes > Remote notifications
Create and register an APNS authentication key on the Apple developer portal
Upload the key to Firebase (under Project Settings > Cloud messaging in the Firebase Console)
Add the Firebase project's reversed client ID to your app's URL schemes
In your
Info.plist
, setFirebaseAppDelegateProxyEnabled
toNO
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
Occlusion Material or Hold-Out Shader in Arkit and Scenekit
Delete All Characters After a Certain Character from a String in Swift
Swiftui Swizzling Disabled by Default, Phone Auth Not Working
How to Set a Specific Default Time for a Date Picker in Swift
Enum with Identical Cases Names with Associated Values of Different Types
In Swift, Why Does Assigning to a Static Variable Also Invoke Its Getter
How to Set an Attributed Title Color for State in Swift
Transparent Nscollectionview Background
How to Convert Int to Byte Array of 4 Bytes in Swift
Getting Data from Findobjectsinbackgroundwithblock in Swift
Class-Only Generic Constraints in Swift
Swift Anyobject Is Not Convertible to String/Int
How to Resolve This Build Issue - Cannot Assign to Property: 'Date' Is a Get Only Property
Access Properties via Subscripting in Swift
How to Take Screen Shot Programmatically (Swift, Spritekit)
Realm Mobile Platform, How to Connect While Offline
How to Use Swift Package Manager with an Existing MACos Project