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
| Property | Type | Description |
|---|---|---|
writeText | (text: string) => void | Copy text to clipboard |
readText | () => Promise<string | null> | Read from clipboard |
isReading | boolean | true while a read is in progress |
errorCode | string | null | Error from the last failed read |
supported | boolean | true if host supports clipboard |
Options
const clipboard = useClipboard({ timeout: 10000 });| Option | Type | Default | Description |
|---|---|---|---|
timeout | number | 5000 | Timeout in ms for read operations |
Error Codes
| Code | Description |
|---|---|
permission_denied | The user denied clipboard access |
unavailable | Clipboard 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:
textcontains the clipboard content (may be empty string), noerrorCode - On failure:
textisnull,errorCodeindicates 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
- Haptic Feedback — Haptic feedback
- Link Management — Open URLs through the host app
- Payments — Accept payments
- Bridge Reference — Full bridge API reference
Last updated on