Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add screen capture feature on iOS #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions ios/Detector.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#import "Detector.h"

@implementation Detector
@implementation Detector {
bool isCapturedNotificationSent;
}

RCT_EXPORT_MODULE();

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

- (void)startObserving {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(sendNotificationToRN:)
name:UIApplicationUserDidTakeScreenshotNotification
object:nil];
selector:@selector(sendNotificationToRN:)
name:UIApplicationUserDidTakeScreenshotNotification
object:nil];

[center addObserver:self
selector:@selector(sendScreenCapturedNotification:)
name:UIScreenCapturedDidChangeNotification
object:nil];

[center addObserver:self
selector:@selector(sendScreenCapturedNotification:)
name:UIApplicationDidBecomeActiveNotification
object:nil];

}

Expand All @@ -26,4 +41,16 @@ - (void)sendNotificationToRN:(NSNotification *)notification {
body:nil];
}

- (void)sendScreenCapturedNotification:(NSNotification *)notification {
if (![UIScreen mainScreen].isCaptured) {
isCapturedNotificationSent = NO;
return;
}
if (isCapturedNotificationSent) {
return;
}
isCapturedNotificationSent = YES;
[self sendEventWithName:@"UIScreenCapturedDidChangeNotification" body:nil];
}

@end
59 changes: 2 additions & 57 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,2 @@
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

const { Detector } = NativeModules;

enum EventsName {
UserDidTakeScreenshot = 'UIApplicationUserDidTakeScreenshotNotification',
}

const detectorEventEmitter = new NativeEventEmitter(Detector);

type Unsubscribe = () => void;

const commonAddScreenshotListener = (listener: () => void): Unsubscribe => {
const eventSubscription = detectorEventEmitter.addListener(
EventsName.UserDidTakeScreenshot,
() => listener(),
{}
);

return () => {
eventSubscription.remove();
};
};

const getListenersCount = (): number => {
return (
// React Native 0.64+
// @ts-ignore
detectorEventEmitter.listenerCount?.(EventsName.UserDidTakeScreenshot) ??
// React Native < 0.64
// @ts-ignore
detectorEventEmitter.listeners?.(EventsName.UserDidTakeScreenshot).length ??
0
);
};

export const addScreenshotListener = Platform.select<
(listener: () => void) => Unsubscribe
>({
default: (): Unsubscribe => () => {},
ios: commonAddScreenshotListener,
android: (listener: () => void): Unsubscribe => {
if (getListenersCount() === 0) {
Detector.startScreenshotDetection();
}

const unsubscribe: Unsubscribe = commonAddScreenshotListener(listener);

return () => {
unsubscribe();

if (getListenersCount() === 0) {
Detector.stopScreenshotDetection();
}
};
},
});
export { addScreenshotListener } from './screen-shot-dector';
export { addScreenCapturedListener } from './screen-captured-detector';
46 changes: 46 additions & 0 deletions src/screen-captured-detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

const { Detector } = NativeModules;

enum EventsName {
UserDidCapturedScreen = "UIScreenCapturedDidChangeNotification"
}

const detectorEventEmitter = new NativeEventEmitter(Detector);

type Unsubscribe = () => void;

const commonAddScreenCapturedListener = (listener: () => void): Unsubscribe => {
const eventSubscription = detectorEventEmitter.addListener(
EventsName.UserDidCapturedScreen,
() => listener(),
{}
);

return () => {
eventSubscription.remove();
};
};

// const getListenersCount = (): number => {
// return (
// // React Native 0.64+
// // @ts-ignore
// detectorEventEmitter.listenerCount?.(EventsName.UserDidCapturedScreen) ??
// // React Native < 0.64
// // @ts-ignore
// detectorEventEmitter.listeners?.(EventsName.UserDidCapturedScreen).length ??
// 0
// );
// };

export const addScreenCapturedListener = Platform.select<
(listener: () => void) => Unsubscribe
>({
default: (): Unsubscribe => () => {},
ios: commonAddScreenCapturedListener,
android: (listener: () => void): Unsubscribe => {
// TODO: add Android screen capture support
return () => {};
},
});
58 changes: 58 additions & 0 deletions src/screen-shot-dector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

const { Detector } = NativeModules;

enum EventsName {
UserDidTakeScreenshot = 'UIApplicationUserDidTakeScreenshotNotification',
UserDidCapturedScreen = "UIScreenCapturedDidChangeNotification"
}

const detectorEventEmitter = new NativeEventEmitter(Detector);

type Unsubscribe = () => void;

const commonAddScreenshotListener = (listener: () => void): Unsubscribe => {
const eventSubscription = detectorEventEmitter.addListener(
EventsName.UserDidTakeScreenshot,
() => listener(),
{}
);

return () => {
eventSubscription.remove();
};
};

const getListenersCount = (): number => {
return (
// React Native 0.64+
// @ts-ignore
detectorEventEmitter.listenerCount?.(EventsName.UserDidTakeScreenshot) ??
// React Native < 0.64
// @ts-ignore
detectorEventEmitter.listeners?.(EventsName.UserDidTakeScreenshot).length ??
0
);
};

export const addScreenshotListener = Platform.select<
(listener: () => void) => Unsubscribe
>({
default: (): Unsubscribe => () => {},
ios: commonAddScreenshotListener,
android: (listener: () => void): Unsubscribe => {
if (getListenersCount() === 0) {
Detector.startScreenshotDetection();
}

const unsubscribe: Unsubscribe = commonAddScreenshotListener(listener);

return () => {
unsubscribe();

if (getListenersCount() === 0) {
Detector.stopScreenshotDetection();
}
};
},
});