feature(ios): Add screenshot and screen capture block

Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com>
This commit is contained in:
Mohamed Javid 2024-06-29 21:29:34 +05:30
parent 3e268936f6
commit 5062b07934
No known key found for this signature in database
GPG Key ID: 9B8D7DD7EF02CF1D
3 changed files with 84 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#import "React/RCTEventDispatcher.h"
#import "Statusgo.h"
#import "Utils.h"
#import "UIHelper.h"
@implementation EncryptionUtils
@ -101,6 +102,13 @@ RCT_EXPORT_METHOD(setBlankPreviewFlag:(BOOL *)newValue)
[userDefaults setBool:newValue forKey:@"BLANK_PREVIEW"];
[userDefaults synchronize];
if (newValue) {
[UIHelper addScreenshotBlock];
} else {
[UIHelper removeScreenshotBlock];
}
}
RCT_EXPORT_METHOD(hashTransaction:(NSString *)txArgsJSON

View File

@ -5,4 +5,7 @@
@interface UIHelper : NSObject <RCTBridgeModule>
+ (void)addScreenshotBlock;
+ (void)removeScreenshotBlock;
@end

View File

@ -8,6 +8,9 @@ RCT_EXPORT_MODULE();
#pragma mark - only android methods
UITextField *textField;
UIView *guardView;
RCT_EXPORT_METHOD(setSoftInputMode: (NSInteger) i) {
#if DEBUG
NSLog(@"setSoftInputMode() works only on Android");
@ -35,5 +38,75 @@ RCT_EXPORT_METHOD(clearStorageAPIs) {
}
}
+ (void) addScreenshotBlock {
// create new secure text field and append to the window layer to prevent screenshot
if (textField == nil) {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
textField = [[UITextField alloc] initWithFrame:window.frame];
textField.translatesAutoresizingMaskIntoConstraints = NO;
[textField setTextAlignment:NSTextAlignmentCenter];
[textField setUserInteractionEnabled: NO];
[window makeKeyAndVisible];
[window.layer.superlayer addSublayer:textField.layer];
if (textField.layer.sublayers.firstObject) {
[textField.layer.sublayers.firstObject addSublayer: window.layer];
}
}
[textField setSecureTextEntry: TRUE];
[textField setBackgroundColor: [UIColor blackColor]];
// create event listener for screen captures and add a view to prevent the app content from exposed if recorded
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[center removeObserver:self
name:UIScreenCapturedDidChangeNotification
object:nil];
[center addObserverForName:UIScreenCapturedDidChangeNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *notification) {
if (UIScreen.mainScreen.captured){
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (guardView == nil) {
guardView = [[UIView alloc]initWithFrame:window.frame];
guardView.backgroundColor = [UIColor blackColor];
guardView.alpha = 0;
}
[window addSubview:guardView];
[window bringSubviewToFront:guardView];
[UIView animateWithDuration:0.5 animations:^{
guardView.alpha = 1;
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
guardView.alpha = 0;
} completion:^(BOOL finished) {
[guardView removeFromSuperview];
}];
}
}];
}
+ (void) removeScreenshotBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (textField != nil) {
[textField setSecureTextEntry: FALSE];
[textField setBackgroundColor: [UIColor clearColor]];
CALayer *textFieldLayer = textField.layer.sublayers.firstObject;
if ([window.layer.superlayer.sublayers containsObject:textFieldLayer]) {
[textFieldLayer removeFromSuperlayer];
}
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self
name:UIScreenCapturedDidChangeNotification
object:nil];
}
@end