0

I have a class and i try to present an UIAlertController directly in it if there is an error. Is it possible to do that in SwiftUI?

My code (obviously not working) :

import Firebase
import Combine

class TestFirebaseAlertClass: ObservableObject  {
    var didChange = PassthroughSubject<TestFirebaseAlertClass, Never>()
    
    func resetPassword(email: String, password: String, handler: @escaping AuthDataResultCallback) {
        Auth.auth().sendPasswordReset(withEmail: email, completion: { error in
            
            if let error = error {
                let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
                view.present(alert, animated: true, completion: nil)
            }
            
        })
    }
    
}


Flincorp
  • 751
  • 9
  • 22
  • You can't present a `UIAlertController` but you can present an `Alert` there are different ways to do this. but the easier is probably to control the required `isPresented` variable in the `ViewModel` and set it to `true` when you have set your alert variable. – lorem ipsum Jun 04 '21 at 17:49
  • I've already done this in another test but I'd really like to control the appearance of the alert in the class. – Flincorp Jun 04 '21 at 18:22

0 Answers0