Hi,
I have a view that should do the following:
- Set up and confirm a passcode upon account creation.
- Verify passcode if signing in.
- Reset passcode if prompted.
With the code below, functions 1 and 2 are working. However, I'm having an issue with function 3. I am able to declare the reset
UserDefault on a previous view so that the proper logic occurs, which is to read in the input, and then confirm it. However, it is not working as intended. In this code here:
else if reset {
UserDefaults.standard.set(passcode, forKey: "new-passcode")
UserDefaults.standard.set(false, forKey: "reset-passcode")
passcode = ""
}
I store the new passcode, set reset to false, and clear the passcode so it can be entered again to confirm. The code does not run as intended however. The title does not change and I'm unsure if it is actually storing the passcode. And, when re-entering, it does not change the view as it should by setting view = .someView.
I'm assuming there is just flaw in my logic but I'm not sure how to resolve this. Below is the full code. Please let me know if any further clarification is needed.
struct EnterPasscode: View {
@State var title = ""
@State var passcode = ""
@State var message = ""
@State var buttonState = false
@Binding var view: Views
var body: some View {
Group {
Spacer()
.frame(height: 50)
Text(title)
.font(.system(size: 36))
.multilineTextAlignment(.center)
.frame(height: 50)
Spacer()
if passcode != "" {
Text("\(passcode)")
.font(.system(size: 24))
.frame(height: 25)
} else {
Spacer()
.frame(height: 25)
}
Spacer()
.frame(height: 50)
PasscodeKeypad(passcode: $passcode)
Spacer()
if message != "" {
Text(message)
.frame(height: 25)
.foregroundStyle(Color.red)
} else {
Spacer()
.frame(height: 25)
}
Spacer()
WideButton(text: "Continue", buttonFunction: .functional, openView: .enterPasscode, view: .constant(.enterPasscode), buttonState: $buttonState)
.onChange(of: buttonState) { oldState, newState in
if buttonState {
let passcodeSet = UserDefaults.standard.bool(forKey: "passcode-set")
let storedPasscode = UserDefaults.standard.string(forKey: "passcode")
let reset = UserDefaults.standard.bool(forKey: "passcode-reset")
let newPasscode = UserDefaults.standard.string(forKey: "new-passcode")
print(reset)
if passcode.count == 4 {
if storedPasscode == nil {
if newPasscode == nil {
UserDefaults.standard.set(passcode, forKey: "new-passcode")
passcode = ""
} else if passcode == newPasscode {
UserDefaults.standard.set(passcode, forKey: "passcode")
UserDefaults.standard.set(true, forKey: "passcode-set")
view = .someView
}
} else if reset {
UserDefaults.standard.set(passcode, forKey: "new-passcode")
UserDefaults.standard.set(false, forKey: "reset-passcode")
passcode = ""
} else if newPasscode != nil {
if passcode == newPasscode {
UserDefaults.standard.set(passcode, forKey: "passcode")
view = .someView
}
}
}
checkPasscodeStatus()
buttonState = false
}
}
Spacer()
if !UserDefaults.standard.bool(forKey: "passcode-reset") && UserDefaults.standard.bool(forKey: "passcode-set") {
Button(action: {
view = .verifyPhone
}) {
Text("Forgot passcode?")
.foregroundStyle(.black)
}
}
Spacer()
.frame(height: 25)
}
.onAppear() {
checkPasscodeStatus()
}
.frame(width: UIScreen.main.bounds.width - 50)
}
func checkPasscodeStatus() {
let passcodeSet = UserDefaults.standard.bool(forKey: "passcode-set")
let storedPasscode = UserDefaults.standard.string(forKey: "passcode")
let reset = UserDefaults.standard.bool(forKey: "passcode-reset")
if reset {
title = "Enter new passcode"
} else if passcodeSet {
title = "Enter Passcode"
} else if storedPasscode != nil && storedPasscode != "" {
title = "Confirm Passcode"
} else {
title = "Select a 4 Digit Passcode"
}
}
}
struct WideButton: View {
@State var text: String
@State var buttonFunction: ButtonType
@State var openView: Views?
@Binding var view: Views
@Binding var buttonState: Bool
var body: some View {
Button(action: {
buttonPressed()
}, label: {
ZStack {
RoundedRectangle(cornerRadius: 5)
.fill(Color.black)
.frame(width: UIScreen.main.bounds.width - 50, height: 50)
Text(text)
.foregroundColor(.white)
}
})
}
func buttonPressed() {
switch buttonFunction {
case .openView: view = openView!
case .functional: buttonState = true
}
}
}
enum ButtonType {
case openView
case functional
}