SwiftUI 4: Strange behaviour of TextField for URL?

Viewed 61

On iOS 16 beta with Xcode 14 beta 1+2, I experience an unexpected behaviour of a TextField if a URL shall be handled.

This is the code snippet:

struct Editor: View {
    
    @State var data: ProfileItem // it has a property link: URL?
    
    var body: some View {
        
        Form {
            
            TextField("Link",
                      value: $data.link,
                      format: .url)
                .keyboardType(.URL)
                .textInputAutocapitalization(.never)
                .disableAutocorrection(true)
                .onChange(of: data.link, perform: {
                    debugPrint("--- ", $0)
                })
        }
    }
}

Observation 1:

When deleting a url like https://blablabla.com/home char-by-char with backspace from the end, then the debugPrint shows the remaining URL in sync with the device until the slash behind .com is erased. Then, on the device https://blablabla is presented, but the debugPrint still shows https://blablabla.com/.

"--- " https://blablabla.com/hom
"--- " https://blablabla.com/ho
"--- " https://blablabla.com/h
"--- " https://blablabla.com/           <<< from here on, the input field content and the debugPrint output diverge, until...
"--- " https://putput.com/              <<< this slash appears

Observation 2:

When deleting the url on the device down to https:// and entering then something else like https://putput.com, the debugPrint still shows the previous URL, until a slash is put behind the new value: https://putput.com/.

This is somehow strange, as I'd expect that a syntactically correct URL should be accepted and lead to a change even if the closing slash is not applied.

So:

(a) Is it correct that the TextField with the .url setup always requires a slash at the end to recognize a changed URL base value?

(b) Is there a possibility to accept a base URL like https://blabla.com even if the user does NOT apply the closing slash?

Thanks for any suggestions!

0 Answers
Related