Core clipboard text functionality supporting plain text and HTML formats with async operations and permission handling. On Web platforms, these operations may prompt users for clipboard access permissions.
Retrieves text content from the user's clipboard with optional format preferences.
/**
* Gets the content of the user's clipboard. On web, this prompts the user
* to grant permission to "see text and images copied to the clipboard."
* @param options - Options for the clipboard content to be retrieved
* @returns Promise that resolves to the clipboard content
*/
function getStringAsync(options?: GetStringOptions): Promise<string>;
interface GetStringOptions {
/** The target format of the clipboard string to be converted to, if possible */
preferredFormat?: StringFormat;
}
enum StringFormat {
PLAIN_TEXT = 'plainText',
HTML = 'html'
}Usage Examples:
import * as Clipboard from "expo-clipboard";
// Get plain text (default)
const text = await Clipboard.getStringAsync();
// Get HTML content if available, fallback to plain text
const htmlContent = await Clipboard.getStringAsync({
preferredFormat: Clipboard.StringFormat.HTML
});Sets text content to the user's clipboard with optional format specification.
/**
* Sets the content of the user's clipboard
* @param text - The string to save to the clipboard
* @param options - Options for the clipboard content to be set
* @returns Promise resolving to boolean on web (success indicator), always true on iOS/Android
*/
function setStringAsync(text: string, options?: SetStringOptions): Promise<boolean>;
interface SetStringOptions {
/** The input format of the provided string. Helps other applications interpret the content properly */
inputFormat?: StringFormat;
}Usage Examples:
import * as Clipboard from "expo-clipboard";
// Set plain text
await Clipboard.setStringAsync("Hello, world!");
// Set HTML content
await Clipboard.setStringAsync(
"<p><strong>Bold text</strong> and <em>italic text</em></p>",
{ inputFormat: Clipboard.StringFormat.HTML }
);
// Check if operation succeeded (mainly useful on Web)
const success = await Clipboard.setStringAsync("Copy this text");
if (success) {
console.log("Text copied successfully");
}Legacy synchronous method for setting clipboard content. Use setStringAsync instead.
/**
* Sets the content of the user's clipboard
* @deprecated Use setStringAsync() instead
* @param text - The string to save to the clipboard
* @returns void on native platforms, boolean on web
*/
function setString(text: string): void;Checks whether the clipboard contains text content, including both plain text and rich text formats.
/**
* Returns whether the clipboard has text content. Returns true for both plain text and rich text (HTML).
* On web, this requires user permission to "see text and images copied to the clipboard"
* @returns Promise that resolves to true if clipboard has text content, false otherwise
*/
function hasStringAsync(): Promise<boolean>;Usage Examples:
import * as Clipboard from "expo-clipboard";
// Check if clipboard has text before attempting to read
if (await Clipboard.hasStringAsync()) {
const text = await Clipboard.getStringAsync();
console.log("Clipboard contains:", text);
} else {
console.log("Clipboard is empty or contains non-text content");
}
// Conditional UI updates
const hasText = await Clipboard.hasStringAsync();
setPasteButtonEnabled(hasText);All text operations may throw UnavailabilityError if the clipboard functionality is not available on the current platform or if required permissions are denied.
import * as Clipboard from "expo-clipboard";
import { UnavailabilityError } from 'expo-modules-core';
try {
const text = await Clipboard.getStringAsync();
} catch (error) {
if (error instanceof UnavailabilityError) {
console.log("Clipboard not available on this platform");
}
}setStringAsync returns boolean success indicator on Web, always true on native platforms