UIPasteboard class to let a user to share data from one place to another within your app, and from your app to other apps. For sharing data with any other app, use the systemwide general pasteboard; for sharing data with another app from your team—that has the same team ID as the app to share from—use named pasteboards.
- iOS 3.0+
In typical usage, an object in your app writes data to a pasteboard when the user requests a copy, cut, or duplicate operation on a selection in the user interface. Another object in the same or different app then reads that data from the pasteboard and presents it to the user at a new location; this usually happens when the user requests a paste operation.
The General Pasteboard and Named Pasteboards
UIPasteboard class provides methods for reading and writing individual pasteboard items, as well as methods for reading and writing multiple pasteboard items at once. See Getting and Setting Single Pasteboard Items and Getting and Setting Multiple Pasteboard Items .
The data to be written to a pasteboard can be in either of two forms:
If the data can be represented by an object—such as
NSURL—it can be represented as a value; use a method such as
setValue(_:forPasteboardType:)to write it to the pasteboard.
If the data is binary, use the
setData(_:forPasteboardType:)method to write it to the pasteboard.
UIPasteboard class provides convenience methods for writing and reading strings, images, URLs, and colors to and from single or multiple pasteboard items, as described in Getting and Setting Pasteboard Items of Standard Data Types.
Starting in iOS 10, the
UIPasteboard class provides properties for directly checking whether specific data types are present on a pasteboard, described in Checking for Data Types on a Pasteboard. Use these properties, rather than attempting to read pasteboard data, to avoid causing the system to needlessly attempt to fetch data before it is needed or when the data might not be present. For example, you can use the new
hasStrings property to determine whether to present a string-data Paste option in the user interface, using code like this:
Pasteboard Items and Representation Types
When you write an object to a pasteboard, the pasteboard stores it as a pasteboard item. A pasteboard item is one or more key-value pairs in which the key identifies the representation type (sometimes called a pasteboard type) of the value.
A Uniform Type Identifier (UTI) is frequently used as the key for a representation type. For example, you could use the
kUTTypeJPEG UTI (a constant for
public.jpeg) as a representation type key for JPEG data.
For a discussion of Uniform Type Identifiers (UTIs) and a list of common ones, see Uniform Type Identifiers Overview.
Your app is free to use any string to name a representation type; however, for app-specific data types, it is recommended that you use reverse-DNS notation to ensure the uniqueness of the type (for example,
You can provide flexibility for data sharing by providing, during a Copy or Cut operation, multiple representation types for a pasteboard item. Various contexts within your app or other apps can then make use of an appropriate representation type. For example, when a user copies an image, your app could write multiple representation types, such as in the PNG, JPEG, and GIF data formats, to a pasteboard. If the original image was in PNG format but the receiving app can handle only GIF images, it can still use the pasteboard data.
For more about representation types, read the discussion for the
types instance method.
Using Pasteboards With Other Objects
UIPasteboard class is central to Copy, Paste, and Duplicate operations, you can employ protocols and instances of other UIKit classes in these operations as well:
UIMenuController— Displays a menu with Copy, Cut, Paste, Select, and Select All commands above or below the selection.
UIResponderStandardEditActions — Responders implement methods declared in this informal protocol to handle the chosen menu commands (for example,
A typical app that implements copy, paste, and duplicate operations also manages and presents related selections in its user interface. In addition, your app must coordinate changes in pasteboard content with changes to its data model, as appropriate for your app.