How do I use clipboard and pasteboard on watchOS using Swift?

On watchOS, you can use the clipboard and pasteboard to handle data sharing. While the capabilities are more restricted compared to iOS, you can still use the `WKInterfaceDevice` and the `UIPasteboard` class for your clipboard needs.

keywords: watchOS, clipboard, pasteboard, Swift, UIPasteboard, WKInterfaceDevice
description: Learn how to utilize clipboard and pasteboard functionalities in your watchOS applications using Swift, including handling text and images.

Here’s a simple example demonstrating how to copy a string to the clipboard and subsequently retrieve it:

import WatchKit import UIKit class InterfaceController: WKInterfaceController { @IBAction func copyToClipboard() { let stringToCopy = "Hello from watchOS!" UIPasteboard.general.string = stringToCopy print("Copied to clipboard: \(stringToCopy)") } @IBAction func pasteFromClipboard() { if let pastedString = UIPasteboard.general.string { print("Pasted from clipboard: \(pastedString)") } else { print("No string found in clipboard.") } } }

keywords: watchOS clipboard pasteboard Swift UIPasteboard WKInterfaceDevice