@exodus/react-native-screenshot-detector 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ios.js CHANGED
@@ -1,51 +1,49 @@
1
1
  import { NativeEventEmitter, NativeModules } from 'react-native'
2
2
 
3
3
  const { RNScreenshotDetector } = NativeModules
4
+
4
5
  const eventEmitter = new NativeEventEmitter(RNScreenshotDetector)
5
6
 
6
7
  const SCREENSHOT_EVENT = 'ScreenshotTaken'
7
8
  const SCREEN_RECORDING_EVENT = 'ScreenRecordingChanged'
8
9
 
9
10
  const subscribe = (cb) => {
10
- const sub = eventEmitter.addListener(SCREENSHOT_EVENT, (data) => {
11
- cb(data)
12
- })
13
- return () => {
14
- sub.remove()
15
- }
16
- }
17
-
18
- const subscribeToScreenRecording = (cb) => {
19
- if (RNScreenshotDetector.subscribeToScreenRecording) {
20
- RNScreenshotDetector.subscribeToScreenRecording()
11
+ if (RNScreenshotDetector && RNScreenshotDetector.subscribeToScreenshotAndScreenRecording) {
12
+ RNScreenshotDetector.subscribeToScreenshotAndScreenRecording()
21
13
  }
22
14
 
23
- const sub = eventEmitter.addListener(SCREEN_RECORDING_EVENT, (data) => {
15
+ const sub = eventEmitter.addListener(SCREENSHOT_EVENT, (data) => {
24
16
  cb(data)
25
17
  })
26
18
  return () => {
27
19
  sub.remove()
28
- if (RNScreenshotDetector.unsubscribeFromScreenRecording) {
29
- RNScreenshotDetector.unsubscribeFromScreenRecording()
20
+
21
+ if (RNScreenshotDetector && RNScreenshotDetector.unsubscribeFromScreenshotAndScreenRecording) {
22
+ RNScreenshotDetector.unsubscribeFromScreenshotAndScreenRecording()
30
23
  }
31
24
  }
32
25
  }
33
26
 
34
27
  const disableScreenshots = () => {
35
- if (RNScreenshotDetector.disableScreenshots) {
28
+ if (RNScreenshotDetector && RNScreenshotDetector.disableScreenshots) {
36
29
  RNScreenshotDetector.disableScreenshots()
37
30
  }
38
31
  }
39
32
 
40
33
  const enableScreenshots = () => {
41
- if (RNScreenshotDetector.enableScreenshots) {
34
+ if (RNScreenshotDetector && RNScreenshotDetector.enableScreenshots) {
42
35
  RNScreenshotDetector.enableScreenshots()
43
36
  }
44
37
  }
45
38
 
46
39
  const isScreenRecording = async () => {
47
- if (RNScreenshotDetector.isScreenRecording) {
48
- return RNScreenshotDetector.isScreenRecording()
40
+ if (RNScreenshotDetector && RNScreenshotDetector.isScreenRecording) {
41
+ try {
42
+ const result = await RNScreenshotDetector.isScreenRecording()
43
+ return result
44
+ } catch (error) {
45
+ return false
46
+ }
49
47
  } else {
50
48
  return false
51
49
  }
@@ -53,7 +51,6 @@ const isScreenRecording = async () => {
53
51
 
54
52
  const ScreenshotDetector = {
55
53
  subscribe,
56
- subscribeToScreenRecording,
57
54
  disableScreenshots,
58
55
  enableScreenshots,
59
56
  isScreenRecording,
@@ -12,7 +12,6 @@
12
12
  {
13
13
  id screenshotObserver;
14
14
  id screenRecordingObserver;
15
- UIView *securityOverlay;
16
15
  BOOL isProtectionEnabled;
17
16
  }
18
17
 
@@ -24,21 +23,26 @@ RCT_EXPORT_MODULE();
24
23
 
25
24
  - (void)startObserving {
26
25
  NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
27
- screenshotObserver = [[NSNotificationCenter defaultCenter]
28
- addObserverForName:UIApplicationUserDidTakeScreenshotNotification
29
- object:nil
30
- queue:mainQueue
31
- usingBlock:^(NSNotification *notification) {
32
- [self screenshotDetected:notification];
33
- }];
34
26
 
35
- screenRecordingObserver = [[NSNotificationCenter defaultCenter]
36
- addObserverForName:UIScreenCapturedDidChangeNotification
37
- object:nil
38
- queue:mainQueue
39
- usingBlock:^(NSNotification *notification) {
40
- [self screenRecordingChanged:notification];
41
- }];
27
+ if (screenshotObserver == nil) {
28
+ screenshotObserver = [[NSNotificationCenter defaultCenter]
29
+ addObserverForName:UIApplicationUserDidTakeScreenshotNotification
30
+ object:nil
31
+ queue:mainQueue
32
+ usingBlock:^(NSNotification *notification) {
33
+ [self screenshotDetected:notification];
34
+ }];
35
+ }
36
+
37
+ if (screenRecordingObserver == nil) {
38
+ screenRecordingObserver = [[NSNotificationCenter defaultCenter]
39
+ addObserverForName:UIScreenCapturedDidChangeNotification
40
+ object:nil
41
+ queue:mainQueue
42
+ usingBlock:^(NSNotification *notification) {
43
+ [self screenRecordingChanged:notification];
44
+ }];
45
+ }
42
46
  }
43
47
 
44
48
  - (void)stopObserving {
@@ -60,9 +64,9 @@ RCT_EXPORT_MODULE();
60
64
  }
61
65
 
62
66
  - (void)screenRecordingChanged:(NSNotification *)notification {
67
+ BOOL isRecording = [UIScreen mainScreen].isCaptured;
68
+
63
69
  if (screenRecordingObserver != nil) {
64
- BOOL isRecording = [UIScreen mainScreen].isCaptured;
65
-
66
70
  [self sendEventWithName:@"ScreenRecordingChanged" body:@{@"isRecording": @(isRecording)}];
67
71
  }
68
72
  }
@@ -87,11 +91,12 @@ RCT_EXPORT_METHOD(isScreenRecording:(RCTPromiseResolveBlock)resolve
87
91
  resolve(@(isRecording));
88
92
  }
89
93
 
90
- RCT_EXPORT_METHOD(subscribeToScreenRecording) {
94
+ // Clear and explicit method names
95
+ RCT_EXPORT_METHOD(subscribeToScreenshotAndScreenRecording) {
91
96
  [self startObserving];
92
97
  }
93
98
 
94
- RCT_EXPORT_METHOD(unsubscribeFromScreenRecording) {
99
+ RCT_EXPORT_METHOD(unsubscribeFromScreenshotAndScreenRecording) {
95
100
  [self stopObserving];
96
101
  }
97
102
 
@@ -130,6 +135,7 @@ RCT_EXPORT_METHOD(unsubscribeFromScreenRecording) {
130
135
  UIWindow *keyWindow = nil;
131
136
 
132
137
  NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
138
+
133
139
  for (UIScene *scene in connectedScenes) {
134
140
  if ([scene isKindOfClass:[UIWindowScene class]]) {
135
141
  UIWindowScene *windowScene = (UIWindowScene *)scene;
@@ -143,18 +149,6 @@ RCT_EXPORT_METHOD(unsubscribeFromScreenRecording) {
143
149
  }
144
150
  }
145
151
 
146
- if (keyWindow == nil) {
147
- keyWindow = [UIApplication sharedApplication].keyWindow;
148
- if (keyWindow == nil) {
149
- for (UIWindow *window in [UIApplication sharedApplication].windows) {
150
- if (window.isKeyWindow) {
151
- keyWindow = window;
152
- break;
153
- }
154
- }
155
- }
156
- }
157
-
158
152
  return keyWindow;
159
153
  }
160
154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/react-native-screenshot-detector",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "detect when the user takes a screenshot",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"