DEV Community

Clipboard paste quirks on iOS Safari: the paste event that fires differently on a phone

When I built the paste handling on my downloader, the desktop version took about ten minutes. A plain input, a paste event listener, read event.clipboardData.getData('text'), tidy the URL, done.

Then I opened it on an iPhone and spent the rest of the afternoon learning that Safari on iOS does not play by the same rules.

The paste event that sometimes never fires

On desktop Chrome, long-pressing or right-clicking into an input and choosing Paste always fires paste before the text lands. On iOS Safari, pasting from the context menu into my link box did fire it, but pasting via the keyboard's own Paste button above the keys did not always do so in the same way, and when the user tapped the floating "Paste" suggestion that appears above the keyboard, the text arrived without the event my handler was listening for.

My code that cleaned the URL on paste simply never ran, and the raw link went straight to the resolver with a tracking parameter still attached.

What the clipboard actually hands you

On desktop, event.clipboardData is there in the paste event and getData('text/plain') returns the string. On iOS, the object exists too, but Safari is pickier about when it is populated.

If the paste comes from a share-sheet link copied in the TikTok app, the string often arrives with extra parameters and sometimes with a trailing newline. My trim step caught it, but only after I found it by logging the raw pasted value on a real phone.

Why navigator.clipboard.readText() is a dead end

On desktop it works from a click handler after the user grants permission once. On iOS Safari it is effectively a dead end for a plain website: the API exists, but the permission prompt behaves differently and the call can be rejected with no useful error, leaving the user staring at a button that does nothing.

I dropped the readText approach entirely. The paste event is the only honest path on iOS, because the user is the one doing the pasting, and Safari trusts that gesture.

The fix: clean on both paste and input

So the fix was to stop relying on the paste event alone. The link box now cleans its value in two places: on paste, and on input.

The input event fires however the text arrived, keyboard Paste button or context menu or autofill, so the trimming, the tracking-parameter strip, and the short-link expansion all run there. The paste handler stays for the one thing only it can do: reading the clipboard before the text lands, so I can check the platform early and show a hint if the link is unsupported.

There is a catch with cleaning on input, and it is an iOS one too. If you rewrite the input value while the user is still typing, Safari can jump the cursor to the end of the field, which is maddening on a long link. I only rewrite the value when it actually changed, and only after a paste or when the field loses focus, never on every keystroke. The fast typist never notices, and the pasted link still gets cleaned.

One more small thing: iOS Safari will not let a page read the clipboard without a user gesture, but it also will not fire paste on a readonly input. My link box is a normal editable input with a real <label>, which sounds obvious, but it is the kind of detail that matters when half your visitors are on a phone and the whole tool is one box.

Test on a real iPhone

The lesson is boring and useful: test the clipboard on a real iPhone, not in a desktop browser with the device toolbar switched on. The emulator shows you the layout; it does not show you the paste event refusing to fire.

I run a short checklist on my own phone before calling any change to that box live, which is how the tool on ramrom.io keeps working for visitors who paste a link from an app share sheet and expect it to just work.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.