How to copy text to clipboard (UIPasteboard) in Swift?
Hello everyone! I'm a beginner in iOS development with Swift, and I've run into what seems like a simple problem. I need that when a button is pressed, text from a text field (UITextField) or just a string that I set programmatically is copied to the clipboard, so the user can then paste it into another app.
I found the UIPasteboard class in the documentation, but there are very few code examples in Swift, mostly in Objective-C. I tried writing something like:
```swift
@IBAction func copyButtonTapped(_ sender: UIButton) {
let textToCopy = myTextField.text
UIPasteboard.general.string = textToCopy
}
```
But it didn't work — nothing gets copied. Maybe I'm accessing the `string` property incorrectly? Or do I need to do some additional setup? I also heard that in iOS...
No answers yet.
Log in to leave an answer
Blogs
Top 10 C# Frameworks for Web Development in 2025