Skip to content

Fix iOS clipboard listener events#296

Open
solaoi wants to merge 1 commit into
react-native-clipboard:masterfrom
solaoi:fix/ios-clipboard-addlistener-super
Open

Fix iOS clipboard listener events#296
solaoi wants to merge 1 commit into
react-native-clipboard:masterfrom
solaoi:fix/ios-clipboard-addlistener-super

Conversation

@solaoi

@solaoi solaoi commented Dec 16, 2025

Copy link
Copy Markdown

Overview

I noticed that Clipboard.addListener does not fire on iOS.

The root cause is that RNCClipboard exports addListener / removeListeners as no-op methods, which prevents RCTEventEmitter from tracking the listener count and calling startObserving.

As a result, clipboard change events are not delivered to JavaScript even though UIPasteboardChangedNotification is observed natively.

This PR fixes the issue by delegating both exported methods to the superclass (RCTEventEmitter) implementation so that listener tracking and observation lifecycle work as intended.

Related issue: #231

Test Plan

  1. Build and run the example app (or any RN app using this library) on iOS.
  2. Add a listener:
Clipboard.addListener(() => console.log('clipboard changed'))
  1. Trigger a clipboard update (e.g. copy text via the iOS selection menu).
  2. Verify the callback is invoked on iOS.

RNCClipboard overrode addListener/removeListeners with no-op stubs,
which prevents RCTEventEmitter from tracking listener count and
starting observation. Delegate both methods to the superclass so
Clipboard.addListener works on iOS.
Comment thread ios/RNCClipboard.mm

RCT_EXPORT_METHOD(addListener : (NSString *)eventName) {
// Keep: Required for RN built in Event Emitter Calls.
[super addListener:eventName];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix 🙌
Tested this PR locally, and the clipboard listener works as expected 🚀

Comment thread ios/RNCClipboard.mm
RCT_EXPORT_METHOD(removeListeners : (double)count) {
// Keep: Required for RN built in Event Emitter Calls.
[super removeListeners:count];
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] - removeListeners should take NSInteger (not double) to match RCTEventEmitter signature

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mosoakinyemi
I looked into this and I don't think that's quite right — RCTEventEmitter itself declares removeListeners:(double)count, so the current iOS implementation matches the RN base class:
https://github.com/facebook/react-native/blob/main/packages/react-native/React/Modules/RCTEventEmitter.h

RN keeps the internal listener count as NSInteger, but the public method signature is double.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants