I'm dealing with my login screen. For that, I've defined an @AppStorage property. On change of scenePhase, I toggle it to false, and call FaceIDAuthentication method when it comes to active:
@main
struct MyApp: App {
@AppStorage("appBlocked") private var appBlocked = false
@Environment(\.scenePhase) var phase
...
MainView()
.onChange(of: phase) { (newPhase) in
switch newPhase {
case .active : print("App is active")
//**
if appBlocked {
faceIdAuthentication()
}
case .inactive : print("App is inactive")
case .background : print("App is on background")
appBlocked = true
default: print("default")
}
}
On my MainView (second on the hiearchy), I condition all shown content to this value:
struct MainView: View {
@AppStorage("appBlocked") private var appBlocked = false
var body: some View {
if appBlocked {
LoginView()
.transition(.opacity)
} else {
//App regular views...
I change this value on Login view (password, just in case FaceId fails, or is not on) with:
if inputPassword == password {
withAnimation {
appBlocked = false
}
} else {
incorrectPassword = true
}
And on FaceIdAthentication with:
if sucess {
withAnimation {
appBlocked = false
}
Both login methods (password and FaceId) work like a charm. But animations don't: no animation (opacity of MainView, or the other two on changing the value) at all is used when "unpresenting" LoginView.
Is this the correct way of doing so? Explained here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-and-remove-views-with-a-transition
Is it correct monitoring @AppStore (UserDefaults) for animations to fire? Have I to add any aditional modifier to my LoginView() on MainView call, maybe?