Quick guide showing how to copy text to clipboard in iOS and how to cleverly use it in SwiftUI.
3 min read

How to copy to clipboard in iOS & SwiftUI


Copying text to the clipboard is one of those small but essential features that can greatly improve your iOS app’s user experience. Whether it’s letting users copy a coupon code, a tracking number, or any other piece of text, Swift makes it simple—and SwiftUI makes it even cleaner. In this guide, you’ll learn how to copy text to the clipboard in iOS using UIPasteboard, and how to build a reusable SwiftUI view modifier so you can add this feature anywhere in your app with just one line of code.

Copy Text

You can write to and read from the iOS clipboard with the UIPasteboard class. It has a singleton property called general which represents the shared clipboard across the entire system.

Copy text to clipboard in Swift like this:

UIPasteboard.general.string = "This text was just copied."

Read Clipboard

Read text copied to the clipboard in Swift:

import UIKit

extension UIPasteboard {
    
    static func printClipboard() {
        guard let copiedText = UIPasteboard.general.string else {
            return
        }
        print("Clipboard is: '\(copiedText)'")
    }
    
}

Copy Text in SwiftUI on Tap

Let’s add this code to a SwiftUI view. We’ll create Text view that copies its content to the clipboard when tapped, using the .onTapGesture view modifier:

Text(
    "Text 1"
)
.onTapGesture {
    UIPasteboard.general.string = "Text 1"
    UIPasteboard.printClipboard()
}

Here’s a complete example, with two Text views so you can test that it works:

import SwiftUI

struct ContentView: View {
    let text1 = "Text 1 was copied."
    let text2 = "Text 2 was copied!"
    
    var body: some View {
        VStack {
            Text(
                text1
            )
            .onTapGesture {
                UIPasteboard.general.string = text1
                UIPasteboard.printClipboard()
            }
            
            Text(
                text2
            )
            .onTapGesture {
                UIPasteboard.general.string = text2
                UIPasteboard.printClipboard()
            }
        }
        .padding()
        .copyToClipboard(
            value: text1 + " + " + text2
        )
    }
}

#Preview {
    ContentView()
}

Copy from VStack in SwiftUI

This works well, but it has one drawback: the user has no feedback that the text was copied. You can fix this by showing a popup, alert, or toast notification.

Alternatively, you could utilize the power of .contextMenu view modifier.

Creating ViewCopyModifier

Let’s create a reusable SwiftUI view modifier that can be applied to any view. This modifier returns a .contextMenu containing a Button with a custom title and action that copies text to the clipboard.

import SwiftUI

public extension View {

    func copyToClipboard(value: String) -> some View {
        return contextMenu {
            Button("Copy \(value)") {
                UIPasteboard.general.string = value
                UIPasteboard.printClipboard()
            }
        }
    }

}

Using the modifier

You can use this modifier on any SwiftUI view to add copy-to-clipboard functionality. Here’s a complete example:

import SwiftUI

struct ContentView: View {
    let text1 = "Text 1 was copied."
    let text2 = "Text 2 was copied!"
    
    var body: some View {
        VStack {
            Text(
                text1
            )
            .onTapGesture {
                UIPasteboard.general.string = text1
                UIPasteboard.printClipboard()
            }
            
            Text(
                text2
            )
            .onTapGesture {
                UIPasteboard.general.string = text2
                UIPasteboard.printClipboard()
            }
        }
        .padding()
        .copyToClipboard(
            value: text1 + " + " + text2
        )
    }
}

#Preview {
    ContentView()
}