Skip to Content
React SDKClipboard
View .md

Clipboard

The Alien SDK provides clipboard access through the host app’s native clipboard API. This works reliably across iOS and Android, unlike browser clipboard APIs which are restricted in WebViews.

React Hook

function CopyButton({ text }: { text: string }) { const { writeText, readText, isReading, errorCode, supported } = useClipboard(); if (!supported) { return <p>Update the Alien app to use clipboard features.</p>; } return ( <div> <button onClick={() => writeText(text)}>Copy</button> <button onClick={async () => { const content = await readText(); if (content !== null) { console.log('Clipboard:', content); } }}> {isReading ? 'Reading...' : 'Paste'} </button> {errorCode && <p>Error: {errorCode}</p>} </div> ); }

Return Values

PropertyTypeDescription
writeText(text: string) => voidCopy text to clipboard
readText() => Promise<string | null>Read from clipboard
isReadingbooleantrue while a read is in progress
errorCodestring | nullError from the last failed read
supportedbooleantrue if host supports clipboard

Options

const clipboard = useClipboard({ timeout: 10000 });
OptionTypeDefaultDescription
timeoutnumber5000Timeout in ms for read operations

Error Codes

CodeDescription
permission_deniedThe user denied clipboard access
unavailableClipboard is not available on this device

Bridge API

For non-React apps, use the bridge directly.

Write to Clipboard

Fire-and-forget — no response event.

send('clipboard:write', { text: 'Hello!' });

Read from Clipboard

Uses request-response pattern with clipboard:response.

const response = await request( 'clipboard:read', {}, 'clipboard:response', { timeout: 5000 }, ); if (response.text !== null) { console.log('Clipboard content:', response.text); } else { console.error('Clipboard error:', response.errorCode); }

clipboard:response

{ reqId: string; text: string | null; errorCode?: 'permission_denied' | 'unavailable'; }
  • On success: text contains the clipboard content (may be empty string), no errorCode
  • On failure: text is null, errorCode indicates the reason

Version Requirements

Clipboard methods are available since contract version 0.1.1. Use useIsMethodSupported or isMethodSupported to check:

const { supported } = useIsMethodSupported('clipboard:write');

Next Steps

Last updated on