Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd behavior when using multiple SecureFields following a TextField in a SwiftUI view

Tags:

swiftui

Note: This bug has been fixed in Xcode 13.0 beta 3

I am experiencing very odd behavior when trying to use multiple SecureFields when following a TextField in a view. Attempting to enter text in one of the SecureFields stops at one character with the field turning yellow and displaying "Strong Password", as well as duplicating in the second SecureField. This is occurring on iOS 14.2 in Xcode 12.2 on the Xcode preview and in the simulator.

Here is a minimal example that demonstrates the issue:

struct SecureFieldTestView: View {

    @State var displayName: String = ""
    @State var password = ""
    @State var passwordVerifiation = ""

    var body: some View {
        VStack {
            TextField("Display name", text: $displayName)
            SecureField("Password", text: $password)
            SecureField("Verify Password", text: $passwordVerifiation)

        }
        .padding()
    }
}

struct SecureFieldTestView_Previews: PreviewProvider {
    static var previews: some View {
        SecureFieldTestView()
    }
}

simulator screenshot of issue

The console shows the following errors when running into the simulator:

[AutoFill] Cannot show Automatic Strong Passwords for app bundleID: <REDACTED BY ME> due to error: iCloud Keychain is disabled
[Assert] View <(null):0x0> does not conform to UITextInput protocol

I have tried to wrap the SecureFields into their own VStack{} and wrapping them all into a Form{}, but the issue remains.

Is there something obvious that I am missing or is this a bug in the SDK?

like image 510
Groundstop Avatar asked Nov 14 '20 22:11

Groundstop


People also ask

How to add padding in TextField in SwiftUI?

The . padding() style will add padding around the TextField component. By default padding is set to all for sides of the component.

What is secure text field?

A secure text field is suitable for use as a password-entry object or for any item in which the text value must be kept secret. NSSecureTextField uses NSSecureTextFieldCell to implement its user interface.


2 Answers

I would report a bug to Apple regarding this issue...

However, for the moment here is a workaround to fix this and still use the SecureField without the yellow bar on top..

SecureField("First", text: $password)
    .textContentType(.newPassword)

Just add textContentType for newPassword and that bar won't appear.

like image 197
davidev Avatar answered Oct 22 '22 01:10

davidev


On further investigation, there is another issue that was occurring; the software keyboard would not appear until after some delay. Entering text in the SecureFields before the software keyboard appeared consistantly produced the issue. Waiting for the software keyboard to appear resolves the odd behavior.

Strangely, the delay in the keyboard appearance seems to occur when the device/simulator is not logged into iCloud; the keyboard appearance delay and SecureField issue does not occur when the device is logged in.

like image 43
Groundstop Avatar answered Oct 22 '22 00:10

Groundstop