diff --git a/ios/CaptureProtection.mm b/ios/CaptureProtection.mm index fbec94e..a1c1bda 100644 --- a/ios/CaptureProtection.mm +++ b/ios/CaptureProtection.mm @@ -7,6 +7,7 @@ static int TAG_SCREEN_PROTECTION = -1004; @implementation CaptureProtection { + bool hasListeners; bool hasScreenRecordObserver; bool hasScreenshotObserver; bool isPreventScreenRecord; @@ -52,7 +53,9 @@ - (NSDictionary *)eventMessage: (CaptureProtectionStatus)status { // Observer Event - (void)eventScreenshot: (NSNotification *)notification { - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:CAPTURE_DETECTED]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:CAPTURE_DETECTED]]; + } } - (void)bundleObserver { @@ -79,11 +82,15 @@ - (void)eventScreenRecordWithInit: (NSNotification *)notification init:(BOOL) in if (isPreventScreenRecord) { [self createRecordProtectionScreen]; } - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_START]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_START]]; + } } else { [self removeRecordProtectionScreen]; if (!init) { - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_END]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_END]]; + } } } } @@ -197,6 +204,13 @@ - (void) removeScreenRecordObserver { hasScreenRecordObserver = NO; } } +- (void) startObserving { + hasListeners = YES; +} + +- (void) stopObserving { + hasListeners = NO; +} RCT_REMAP_METHOD(setScreenRecordScreenWithImage, screenImage: (NSDictionary*) screenImage @@ -328,7 +342,9 @@ - (void) removeScreenRecordObserver { [self removeScreenShotObserver]; } isPreventScreenshot = NO; - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + } resolve(@(YES)); } @catch (NSException *e) { @@ -345,7 +361,9 @@ - (void) removeScreenRecordObserver { [self secureScreenshotView:true]; [self addScreenShotObserver]; isPreventScreenshot = YES; - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + } resolve(@(YES)); } @catch (NSException *e) { @@ -365,7 +383,9 @@ - (void) removeScreenRecordObserver { [self removeScreenRecordObserver]; } isPreventScreenRecord = NO; - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + } resolve(@(YES)); } @catch (NSException *e) { @@ -385,7 +405,9 @@ - (void) removeScreenRecordObserver { if (isImmediate) { [self eventScreenRecordWithInit:nil init:true]; } - [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + if (hasListeners) { + [self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]]; + } resolve(@(YES)); } @catch (NSException *e) {