struct Course: Identifiable { let id = UUID().uuidString var name: String var duration: String var category: String }
extension Course { static var sample: [Course] { [ Course(name: "Mastering SwiftUI 3", duration: "1h 20m", category: "SwiftUI"), Course(name: "UIKit in Depth", duration: "2h 30m", category: "UIKit"), Course(name: "Machine Learning by Example", duration: "4h 20m", category: "iOS Machine Learning") ] } }
struct DevTechieHomeView: View { let courses = Course.sample var body: some View { NavigationView { List(courses) { course in VStack(alignment: .leading) { Text(course.name) .font(.title) HStack { Text(course.duration) Spacer() Text(course.category) }.foregroundColor(.secondary) }.padding() .background(RoundedRectangle(cornerRadius: 10).fill(Color.secondary.opacity(0.3))) .listRowSeparator(.hidden) } .listStyle(.plain) .navigationTitle("DevTechie") } } }
struct DevTechieLoginView: View { @State private var isUnlocked = false @State private var failedAuth = "" var body: some View { if isUnlocked { DevTechieHomeView() } else { VStack { Text("Welcome to") Text("DevTechie Courses") .font(.largeTitle) Button(action: /* call authentcation here */ ) { Label("Login with FaceID", systemImage: "faceid") .padding() .background(RoundedRectangle(cornerRadius: 15).foregroundColor(.white)) } Text(failedAuth) .padding() .opacity(failedAuth.isEmpty ? 0.0 : 1.0) } } } }
private func authenticate() { var error: NSError? let laContext = LAContext() if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { let reason = "Need access to authenticate" laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in DispatchQueue.main.async { if success { isUnlocked = true failedAuth = "" } else { print(error?.localizedDescription ?? "error") failedAuth = error?.localizedDescription ?? "error" } } } } else { } }
import SwiftUI import LocalAuthenticationstruct DevTechieLoginView: View { @State private var isUnlocked = false @State private var failedAuth = "" var body: some View { if isUnlocked { DevTechieHomeView() } else { VStack { Text("Welcome to") Text("DevTechie Courses") .font(.largeTitle) Button(action: authenticate) { Label("Login with FaceID", systemImage: "faceid") .padding() .background(RoundedRectangle(cornerRadius: 15).foregroundColor(.white)) } Text(failedAuth) .padding() .opacity(failedAuth.isEmpty ? 0.0 : 1.0) } } } private func authenticate() { var error: NSError? let laContext = LAContext() if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { let reason = "Need access to authenticate" laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in DispatchQueue.main.async { if success { isUnlocked = true failedAuth = "" } else { print(error?.localizedDescription ?? "error") failedAuth = error?.localizedDescription ?? "error" } } } } else { } } }