iOS
Clipboard
Copy Text
iOS Development
Mobile Tips

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.

swift
1import UIKit
2
3let text = "Order number: 4829"
4UIPasteboard.general.string = text

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.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    private let valueLabel: UILabel = {
5        let label = UILabel()
6        label.text = "INV-2026-0042"
7        label.translatesAutoresizingMaskIntoConstraints = false
8        return label
9    }()
10
11    override func viewDidLoad() {
12        super.viewDidLoad()
13        view.backgroundColor = .systemBackground
14        view.addSubview(valueLabel)
15
16        NSLayoutConstraint.activate([
17            valueLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
18            valueLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
19        ])
20
21        navigationItem.rightBarButtonItem = UIBarButtonItem(
22            title: "Copy",
23            style: .plain,
24            target: self,
25            action: #selector(copyValue)
26        )
27    }
28
29    @objc private func copyValue() {
30        UIPasteboard.general.string = valueLabel.text
31    }
32}

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.

swift
1import SwiftUI
2import UIKit
3
4struct CopyView: View {
5    let couponCode = "SPRING-25"
6
7    var body: some View {
8        VStack(spacing: 16) {
9            Text(couponCode)
10                .font(.title2)
11
12            Button("Copy Code") {
13                UIPasteboard.general.string = couponCode
14            }
15        }
16        .padding()
17    }
18}

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.

swift
1@objc private func copyValue() {
2    UIPasteboard.general.string = valueLabel.text
3
4    let alert = UIAlertController(
5        title: nil,
6        message: "Copied to clipboard",
7        preferredStyle: .alert
8    )
9
10    present(alert, animated: true)
11
12    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
13        alert.dismiss(animated: true)
14    }
15}

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.

swift
1import UIKit
2
3if let url = URL(string: "https://example.com/invoice/4829") {
4    UIPasteboard.general.url = url
5}

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.general already 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.

Course illustration
Course illustration

All Rights Reserved.