Copy text to clipboard with iOS
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On iOS, copying text to the clipboard means writing to the system pasteboard. The API is simple, but good clipboard behavior is mostly about timing and UX rather than syntax. You should write to the pasteboard in response to a clear user action and give the user feedback that the copy succeeded.
Use UIPasteboard.general
For most apps, the global pasteboard is all you need. Setting its string property copies plain text that other apps can paste.
That is the essential operation. Once this line runs, the copied text is available for pasting elsewhere on the device.
Copy from a Button Tap in UIKit
Clipboard writes usually belong inside a user-triggered event such as tapping a button or long-pressing a field. That keeps the behavior expected and avoids surprising the user.
This is the normal pattern in UIKit: the user taps "Copy," and the app writes the current text to the pasteboard.
SwiftUI Example
SwiftUI still relies on the same underlying pasteboard API. You just call it from a button action.
The UI framework changes, but the clipboard write stays the same.
Provide User Feedback
Copying silently can feel broken because nothing visible changes on screen. A toast, label change, or alert often makes the interaction clearer.
You do not need an alert specifically, but some visible confirmation is usually worth adding.
Copy More Than Plain Text
UIPasteboard can store more than strings. You can write URLs, images, or collections of typed items if the feature needs richer data. For plain text sharing, string is enough. If the value is a URL, storing it as a URL may be clearer than converting it to raw text first.
That allows other apps to understand the copied content semantically.
Be Careful with Automatic Clipboard Behavior
Programmatically copying without user intent is usually a bad experience. It can also make an app feel intrusive. A better rule is simple: copy in response to a visible user action, and keep the copied data narrowly scoped to what the user expects.
Reading from the pasteboard also deserves care because platform privacy features may surface that access to the user. Even though this article is about writing, the same principle applies: use the pasteboard deliberately rather than casually.
Common Pitfalls
- Copying text automatically on screen load instead of waiting for a user action.
- Forgetting to provide any visual feedback after the copy succeeds.
- Storing data as a plain string when a more specific pasteboard type, such as a URL, would be clearer.
- Assuming clipboard writes require a complex API when
UIPasteboard.generalalready covers the common case. - Mixing clipboard logic into unrelated code instead of keeping it close to the user interaction that triggered it.
Summary
- On iOS, copying text means writing to
UIPasteboard.general. - The simplest plain-text copy is
UIPasteboard.general.string = text. - Put clipboard writes behind explicit user actions such as button taps.
- Add visible feedback so the user knows the copy worked.
- Use richer pasteboard types only when the data really needs them.

