How to Open File Dialog with Swiftui on Platform "Uikit for MAC"

SwiftUI - Use File Picker to Open a file belonging to another IOS Application, then access that file directly

So just for the archive, it turns out that my original Document Picker code was working - and was working correctly - just as I had provided it.

The problem turned out to be the way I was handling the response.

My original wrong code was:

do {
data1 = try NSData.init(
contentsOf: URL.init(fileURLWithPath: fileURL, isDirectory: true)
) as Data
}
catch {
print("Error \(error)")
}

and that was the problem. Turns out that to get the data stream from the file, all I needed to do was:

do {
data1 = try Data(contentsOf: fileURL!)
}
catch {
print("Error \(error)")
}

That was it! When I did that, I was able to read the contents of the file, and correctly upload them via POST to my server.

Thanks to all who read my question, and to Prafulla for their response!

Cannot find UIDocumentPickerViewController in scope

Apparently UIDocumentPickerViewController is not available when building for Mac OS X, and NSOpenPanel seems to be a way to get the necessary functionality.

How to compile a 3rd party library to be used with UIKit For Mac/Catalyst?

Well, after digging around for a while, I found out that if you supply clang with the following target (set the CFLAGS variable before calling configure), it compiles the right version of the library (note the -macabi suffix):

-target x86_64-apple-ios${MIN_IOS_VERSION}-macabi

I also add the minimum os version flag to the macOS version:

-mmacosx-version-min=${MIN_OSX_VERSION}

Here MIN_IOS_VERSION="13.0" and MIN_OSX_VERSION="10.15"

SwiftUI Custom Picker / ComboBox

Finally found MenuButton which does the trick for SwiftUI in MacOS.

MenuButton(label: Title(), content: {
Button(action: {
print("Clicked an item")
}) {
Text("Menu Item Text")
}
})
.menuButtonStyle(BorderlessButtonMenuButtonStyle())

... with using a custom View Title() for my clickable button.

struct Title: View {
var body: some View {
HStack
{
Text("Title")
}
}
}

Autofocus TextField programmatically in SwiftUI


iOS 15

There is a new wrapper called @FocusState that controls the state of the keyboard and the focused keyboard ('aka' firstResponder).

Become First Responder ( Focused )

If you use a focused modifier on the text fields, you can make them become focused, for example, you can set the focusedField property in the code to make the binded textField become active:

Sample Image

Resign first responder ( Dismiss keyboard )

or dismiss the keyboard by setting the variable to nil:

Sample Image

Don't forget to watch the Direct and reflect focus in SwiftUI session from WWDC2021



iOS 13 and 14 (and 15)

Old but working:

Simple wrapper struct - Works like a native:

Note that Text binding support added as requested in the comments

struct LegacyTextField: UIViewRepresentable {
@Binding public var isFirstResponder: Bool
@Binding public var text: String

public var configuration = { (view: UITextField) in }

public init(text: Binding<String>, isFirstResponder: Binding<Bool>, configuration: @escaping (UITextField) -> () = { _ in }) {
self.configuration = configuration
self._text = text
self._isFirstResponder = isFirstResponder
}

public func makeUIView(context: Context) -> UITextField {
let view = UITextField()
view.addTarget(context.coordinator, action: #selector(Coordinator.textViewDidChange), for: .editingChanged)
view.delegate = context.coordinator
return view
}

public func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
switch isFirstResponder {
case true: uiView.becomeFirstResponder()
case false: uiView.resignFirstResponder()
}
}

public func makeCoordinator() -> Coordinator {
Coordinator($text, isFirstResponder: $isFirstResponder)
}

public class Coordinator: NSObject, UITextFieldDelegate {
var text: Binding<String>
var isFirstResponder: Binding<Bool>

init(_ text: Binding<String>, isFirstResponder: Binding<Bool>) {
self.text = text
self.isFirstResponder = isFirstResponder
}

@objc public func textViewDidChange(_ textField: UITextField) {
self.text.wrappedValue = textField.text ?? ""
}

public func textFieldDidBeginEditing(_ textField: UITextField) {
self.isFirstResponder.wrappedValue = true
}

public func textFieldDidEndEditing(_ textField: UITextField) {
self.isFirstResponder.wrappedValue = false
}
}
}

Usage:

struct ContentView: View {
@State var text = ""
@State var isFirstResponder = false

var body: some View {
LegacyTextField(text: $text, isFirstResponder: $isFirstResponder)
}
}

Bonus: Completely customizable

LegacyTextField(text: $text, isFirstResponder: $isFirstResponder) {
$0.textColor = .red
$0.tintColor = .blue
}

Is there a list of absolutely EVERY modifier that is available for SwiftUI?

The easiest way to find the list of all the modifiers in the Xcode search by searching View Modifiers. The Xcode then gives you this list: Link to the Apple ViewModifiers documentation

I think this is the best we can do with standard modifiers. Also, keep in mind, that there are could be more of them in a project that you work on as anyone can add new modifiers to their projects.



Related Topics



Leave a reply



Submit