-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
81 lines (72 loc) · 2.52 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// ContentView.swift
// ATSPro
//
// Created by User2 on 29/04/24.
//
// ContentView.swift
import SwiftUI
import LocalAuthentication
struct ContentView: View {
@State private var isUnlocked = false
@State private var showingAlert = false
@State private var alertMessage = ""
var body: some View {
NavigationView {
VStack {
if isUnlocked {
HomeView() // Your content for when the app is unlocked
} else {
VStack {
Text("LOCKED")
.bold()
.padding()
Button("Authenticate") {
authenticate()
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Authentication Error"), message: Text(alertMessage), dismissButton: .default(Text("OK")))
}
}
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
func authenticate() {
let context = LAContext()
var error: NSError?
// Check if biometrics is available on the device.
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// It's available, so use it.
let reason = "Identify yourself to unlock your account."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
success, authenticationError in
// Ensure we're running on the main thread
DispatchQueue.main.async {
if success {
// Authenticated successfully
self.isUnlocked = true
} else {
// There was a problem
self.alertMessage = authenticationError?.localizedDescription ?? "Unknown error"
self.showingAlert = true
}
}
}
} else {
// No biometrics
self.alertMessage = error?.localizedDescription ?? "Biometrics not available"
self.showingAlert = true
}
}
}
struct AuthenticatedView: View {
var body: some View {
Text("UNLOCKED: Welcome to the app!")
// Your content for the unlocked state
}
}
#Preview {
ContentView()
}