проверьте это:
Мне пришлось кое-что закомментировать и кое-что изменить, потому что ваш пример вообще не компилировался, но вам должно быть понятно, если вы его видите;)
struct DashboardView : View {
var body: some View {
Text("Dashboardview")
}
}
struct LoginView : View {
var body: some View {
Text("LoginView")
}
}
struct FaceLoginView: View {
private var isUnlocked : Bool {
get {
return Bool.random()
}
set {
self.isUnlocked = newValue
}
}
@State var navigateToDashboard = false
@State var navigateToLogin = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DashboardView(), isActive: $navigateToDashboard) { EmptyView().hidden()
}.hidden().frame(height:0)
NavigationLink(destination: LoginView(), isActive: $navigateToLogin) { EmptyView().hidden()
}.hidden().frame(height:0)
Button(action: {
print("face id tapped!")
if self.isUnlocked {
// print("unlocked")
self.navigateToDashboard.toggle()
} else {
// Text("Locked")
self.navigateToLogin.toggle()
}
}) {
HStack {
Image(systemName: "faceid")
.font(.title)
Text("faceid")
.fontWeight(.semibold)
.font(.title)
}
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
// .foregroundColor(.white)
.background(LinearGradient(gradient: Gradient(colors: [Color("DarkGreen"), Color("LightGreen")]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(40)
}
}
}
.onAppear(perform: authenticate)
}
func authenticate() {
// let context = LAContext()
var error: NSError?
// if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// let reason = "Log in to your account by unlocking FaceID."
// context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
// DispatchQueue.main.async {
// if success {
// // authenticated successfully
// self.isUnlocked = true
// } else {
// // there was a problem
// self.isUnlocked = false
// print(error?.localizedDescription ?? "Failed to authenticate")
// }
//
// }
// }
// } else {
// // no biometrics
// print(error?.localizedDescription ?? "Can't evaluate policy")
//
// }
}
}
struct ContentView: View {
var body: some View {
FaceLoginView()
}
}