I don't believe there is a way to do it with pure SwiftUI but I'm still new to the language (day 75/100) so I might be wrong.
I did however find this is a swift package that can give SwiftUI elements access to features from their UIKit or AppKit equivalents:
SwiftUI-Introspect
On the github page, click the "Clone or Download" button then copy the https link provided. It should be this one:
https://github.com/siteline/SwiftUI-Introspect.git
To add this package to your project in XCode, go to:
File > Swift Packages > Add Package Dependency
then paste the link.
You should see the package appear on the left column below your files and under the heading "Swift Package Dependencies"
Now that the package is in your project, you need to add this line to use it in one of your swift files. Add this to ContentView.swift
import Introspect
Now you can add a new vew modifier to your text field to make it act as a first responder.
.introspectTextField { textField in
textField.becomeFirstResponder()
}
The .becomeFirstResponder() function is from UIKit and it forces the text field to become active every time the view is refreshed.
I would also recommend adding this to your text field to prevent it from activating while alerts are showing.
.disabled(showingError)
Keep in mind this solution doesn't seem to work if you have more than one text field as the focus will keep jumping back to the first whenever the view reloads.