React Native - IOS ScreenRecordingListener Native Module error: Bridge is not set

456 Views Asked by At

I've created a module for listening when a screen record event is trigered but I keep getting this error: Bridge is not set. This is probably because you've explicitly synthesized the bridge in ScreenCaptureNotification, even though it's inherited from RCTEventEmitter.'

This is my header file: ScreenRecordingNotification.h

#import <React/RCTBridgeModule.h>
#import <React/RCTBridgeDelegate.h>
#import <React/RCTEventEmitter.h>

#ifndef ScreenCaptureNotification_h
#define ScreenCaptureNotification_h

@interface ScreenCaptureNotification : RCTEventEmitter <RCTBridgeModule>
-(void) isScreenCaptureEnabled:(BOOL)isCaptured;
@end

#endif

and my Objective-c file: ScreenCaptureNotification.m

#import "ScreenRecordingNotification.h"

#import <Foundation/Foundation.h>

#import <React/RCTLog.h>

@implementation ScreenCaptureNotification

+ (id)allocWithZone:(NSZone *)zone {
  static ScreenCaptureNotification *sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
  });
  return sharedInstance;
}

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents {
  return @[@"isScreenCaptureEnabled"];
}

-(void) isScreenCaptureEnabled:(BOOL)isCaptured {
  [self sendEventWithName:@"isScreenCaptureEnabled" body:@{@"value": @(isCaptured)}];
}

@end

Finally I use the module on AppDelegate.m

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"captured"]){
      NSLog(@"Screen Capture is Enabled");
      RCTLog(@"Screen Capture is Enabled");
      if (@available(iOS 11.0, *)) {
        ScreenCaptureNotification *manager = [ScreenCaptureNotification allocWithZone: nil];
        [manager isScreenCaptureEnabled:true];
        
        
      }
    }
}

This code is based on this post's answer prevent screen capture react-native

Can someone help me with this error please? I've been stuck for days

1

There are 1 best solutions below

0
On

Hi was able to resolve this issue by calling the listner method in my componentwillmount function in my class component and it worked.

    let bridge = new NativeEventEmitter(NativeModules.ScreenCaptureNotification);

    this.screenCaptureEnabled = bridge.addListener("isScreenCaptureEnabled",res => { 
      this.setState({ screenCapture: true })
    })
  }

call or use this method in componentwillmount or useEffect if using functional component.