I would like to implement a global copy command for my app. The code below is an attempt at achieving this but does not work.
When the user selects something in the TextField, then using the copy menu should copy that text to the clipboard but when it has nothing selected, then I want to copy "Hallo" and the name to the clipboard.
As you can see, I tried using the onCopyCommand but Copy is always grayed out.
Any ideas on how I can enable Copy on the menu, and handle it when the TextField does not have anything selected?
Code Block struct ContentView: View { @State private var name = "" var body: some View { VStack { Text("Hallo \(name)") TextField("Please enter your name", text: $name) } .onCopyCommand(perform: { print("You got to onCopy") var items = [NSItemProvider]() let stringData = "Hallo \(name)".data(using: .utf8)! items.append(NSItemProvider(item: stringData as NSData, typeIdentifier: kUTTypePlainText as String)) return items }) } }
When the user selects something in the TextField, then using the copy menu should copy that text to the clipboard but when it has nothing selected, then I want to copy "Hallo" and the name to the clipboard.
As you can see, I tried using the onCopyCommand but Copy is always grayed out.
Any ideas on how I can enable Copy on the menu, and handle it when the TextField does not have anything selected?