// // DocsView.swift // Splits // // Created by Isaac Greene on 6/3/22. // import SwiftUI import LocalAuthentication // this file will have some comments. // Most of this code is considered simple enough // to be human-readable without aid, as long as // the reader has a basic understanding of // Swift and/or SwiftUI. let username = "admin" let password = "123" struct DocsView: View { enum Field: Hashable { case username case password } @State private var pass: String = "" @State private var user: String = "" @State private var isUnlocked = false @FocusState private var focusedField: Field? var body: some View { NavigationView { List { NavigationLink("Help", destination: HelpView()) NavigationLink("Change Log", destination: ChangeLog()) Section(header: Text("Features")) { NavigationLink("New", destination: NewFeatures()) NavigationLink("In Progress", destination: InProgressFeatures()) NavigationLink("Deprecated", destination: DeprecatedFeatures()) } Section(header: Text("Known Issues")) { NavigationLink("Recently Resolved", destination: RecentlyResolved()) NavigationLink("High Priority", destination: HighPriority()) NavigationLink("Medium Priority", destination: MediumPriority()) NavigationLink("Low Priority", destination: LowPriority()) } Section(header: Text("App Information")) { NavigationLink("Software License", destination: LicenseView()) Text("Version: Release Candidate 2 (1.0.0)") Text("Release date: 2022-06-11") Text("Start date: 2022-03-25") Link("Built with SwiftUI \(Image(systemName: "swift"))", destination: URL(string: "https://developer.apple.com/xcode/swiftui")!) } Section(header: Text("Login")) { if (isUnlocked) { NavigationLink("Contacts", destination: SecretView()) Button("Log out") { pass = "" user = "" isUnlocked = false } } else { if !isUnlocked { Button("Log in with biometrics") { authenticate() } } SecureField("Username", text: $user) .keyboardType(.alphabet) .textContentType(.username) .submitLabel(.next) .focused($focusedField, equals: .username) SecureField("Password", text: $pass) .keyboardType(.numbersAndPunctuation) .textContentType(.password) .submitLabel(.done) .focused($focusedField, equals: .password) if checkPassword() { NavigationLink("Contacts", destination: SecretView()) } } } } .navigationTitle("Docs") } .onSubmit { switch focusedField { case .username: focusedField = .password default: () } if (pass == password && user == username) { isUnlocked = true } } } func checkPassword() -> Bool { if (pass == password && user == username) { return true } else { return false } } func checkIfUnlocked() { if !isUnlocked { authenticate() } } func authenticate() { let context = LAContext() var error: NSError? // check whether biometric authentication is possible if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { // it's possible, so go ahead and use it let reason = "We need authentication before we can show you sensitive data" context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in // authentication has now completed if success { isUnlocked = true } else { () } } } else { () } } // getting this to work came from // https://www.hackingwithswift.com/books/ios-swiftui/using-touch-id-and-face-id-with-swiftui // a truly epic website and it's helped me with // just about all of my code questions } struct DocsView_Previews: PreviewProvider { static var previews: some View { DocsView() } }