How to copy text to clipboard/pasteboard with Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Copying text to the clipboard on Apple platforms is technically simple, but a production-quality implementation should still validate input and respect privacy. On iOS and iPadOS, UIPasteboard is the standard API, and it works best when wrapped in a small reusable abstraction instead of being called ad hoc from many screens.
Use UIPasteboard.general for Basic Copy
The simplest possible copy action is assigning a string to the general pasteboard.
That is enough for a prototype. In a real app, you usually want to avoid copying empty strings, placeholder values, or sensitive text without an explicit user action.
Wrap Clipboard Access in a Service
A small wrapper makes the behavior consistent across the app.
This gives you one place to add trimming, logging, or policy checks later.
UIKit Example With Validation and Feedback
A copy button should ignore empty values and confirm success to the user.
That feedback step matters. Without it, users often tap again because the interface gave no sign the copy operation worked.
SwiftUI Uses the Same API
SwiftUI can still call UIPasteboard directly from an action closure.
If your app already has a shared architecture layer, use the same clipboard service from SwiftUI rather than scattering pasteboard writes through view code.
Copy Other Types When Appropriate
The pasteboard can store more than plain text. If you have a URL, use the URL property instead of converting it to a string first.
Using the most specific type available makes paste behavior more predictable in other apps.
Treat the Clipboard as Shared State
The system pasteboard is visible outside your app. That means copy actions have privacy implications.
Good rules include:
- do not auto-copy secrets without explicit user action
- trim accidental whitespace before copying
- think carefully before copying tokens, passwords, or one-time codes
- document any feature that reads pasteboard content automatically
Clipboard behavior is not just a UI detail. It is part of the app's trust model.
Common Pitfalls
The most common mistake is copying empty or placeholder text because the source value was never validated.
Another common issue is giving no feedback after the copy action. Developers also sometimes treat the clipboard as harmless temporary storage and forget that other apps and the system can observe or replace pasteboard contents after the copy occurs.
Summary
- Use
UIPasteboard.generalfor standard copy-to-clipboard behavior on iOS. - Wrap pasteboard access in a small service when several screens need copy behavior.
- Trim and validate text before copying it.
- Give the user visible confirmation that the copy succeeded.
- Treat the pasteboard as shared state and be careful with sensitive data.
Related reading
- How to Correctly handle Weak Self in Swift Blocks with Arguments
- How to Correctly handle Weak Self in Swift Blocks with Arguments
- How to correctly save instance state of Fragments in back stack?
- How to correctly subclass UIControl?
- How to count occurrences of an element in a Swift array?
- How to create a circular ImageView in Android?
- How to Create a circular progressbar in Android which rotates on it?
- How to create a colored 1x1 UIImage on the iPhone dynamically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.