iOS Question Prevent screenshot iOS

tsteward

Well-Known Member
Licensed User
Longtime User
Is there a B4i version of this code
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   Dim no As NativeObject = Me
   no.RunMethod("addListener", Null)
End Sub

#if OBJC
- (void)addListener {
  [[NSNotificationCenter defaultCenter] addObserver:self
  selector:@selector(_screenshot_taken)
  name:UIApplicationUserDidTakeScreenshotNotification  object:nil];
}
#end if

Private Sub Screenshot_Taken
   Log("Screenshot taken")
End Sub

I did find this when search www
You cannot directly block UIApplicationUserDidTakeScreenshotNotification from firing to prevent a screenshot; it only notifies you after it happens. To actually prevent screen capture, you must apply isSecureTextEntry to a hidden UITextField, which tricks iOS into masking the screen with black.

Implementation in Objective-C
To secure a specific view, use a UITextField set to secureTextEntry.
#if OBJC
- (void)preventScreenCapture {
// 1. Create a UITextField
UITextField *field = [[UITextField alloc] init];
field.secureTextEntry = YES;

// 2. Add to your current view
[self.view addSubview:field];

// 3. Make the secure layer cover your desired content
// Typically requires setting up constraints to fill the screen/view
field.layer.sublayers.firstObject.delegate = self;

// 4. Hide the text field itself
field.hidden = YES;
}
#endif
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I tried the suggestion with a hidden text view. It doesn't seem to work on iOS 26.

B4X:
TextView1.As(NativeObject).SetField("secureTextEntry", True)

The field itself will not appear in the screenshot (if visible).

Maybe you can hide the content when the app moves to the background or in one of the other page events.
 
Upvote 0
Top