iOS Question ios screenshot block about

Ertan

Active Member
Licensed User
Hello there,
I can block it with these codes while using Android.

B4X:
Dim nativeMe As JavaObject

-----------

nativeMe.InitializeContext
nativeMe.RunMethod("securescreen",Null)
-----------

#If Java

import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void securescreen() {
    this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}

#End If

How can I prevent taking screenshots on ios phones?
Thanks.
 

Ertan

Active Member
Licensed User
Ignore my previous answer. It will not work. The background event is not raised in this case.

You cannot prevent screenshots in iOS.
Is there a code for us to understand that it takes a screenshot?
Like taking msgbox when taking screenshot.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
To detect a screenshot is simple. For example,
1) add to the bottom of Main module

B4X:
#If OBJC
- (void) DetectScreenshot
    {
     NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationUserDidTakeScreenshotNotification
       object: nil queue: mainQueue usingBlock: ^(NSNotification *note) { self._screenshot; }];
    }
#End If

2) Call this subroutine from Application_Start
B4X:
    Dim no As NativeObject = Me
    no.RunMethod ("DetectScreenshot", Null)

3) Add callback function
B4X:
Private Sub Screenshot
    xui.MsgboxAsync("Screenshot", "B4X") ' Or what you want to do
End Sub

I will not surprise, if it will be easy to remove screenshot from device.

But don't remember that bad boy can take another phone and to make a photo.
 
Upvote 0

Ertan

Active Member
Licensed User
To detect a screenshot is simple. For example,
1) add to the bottom of Main module

B4X:
#If OBJC
- (void) DetectScreenshot
    {
     NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationUserDidTakeScreenshotNotification
       object: nil queue: mainQueue usingBlock: ^(NSNotification *note) { self._screenshot; }];
    }
#End If

2) Call this subroutine from Application_Start
B4X:
    Dim no As NativeObject = Me
    no.RunMethod ("DetectScreenshot", Null)

3) Add callback function
B4X:
Private Sub Screenshot
    xui.MsgboxAsync("Screenshot", "B4X") ' Or what you want to do
End Sub

I will not surprise, if it will be easy to remove screenshot from device.

But don't remember that bad boy can take another phone and to make a photo.
Thanks for the code. I run.
How can I take a screenshot of myself without pressing the keys?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Do you mean to make a copy of the screen ? Sure, I saw samples on forum.

Well, from my archives.
Add to OBJC:

B4X:
- (UIImage*) TakeScreenshot
    {
    UIWindow* window;
    if (@available (iOS 13, *))    { window = UIApplication.sharedApplication.windows.firstObject;  } else
        { window = UIApplication.sharedApplication.delegate.window; };
    UIGraphicsBeginImageContextWithOptions (window.bounds.size, NO, 0.0);
    [window.layer renderInContext: UIGraphicsGetCurrentContext ()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext (); // this captures the screenshot
    UIGraphicsEndImageContext ();
    return image;
    }

This function returns Bitmap. You can, for example, to assign it to Imageview:
B4X:
Dim no As NativeObject = Me
ImageView1.Bitmap = no.RunMethod ("TakeScreenshot", Null)

I tested under IOS 13 only, but should work in any 8+ also.
This code does not capture "status bar' in IOS 13 (I guess the reason is that Apple removed status bar window in IOS13+).
Simulator Screen Shot - iPhone 11 Pro Max - 2020-07-17 at 21.49.28.png
 
Last edited:
Upvote 0

Ertan

Active Member
Licensed User
Do you mean to make a copy of the screen ? Sure, I saw samples on forum.

Well, from my archives.
Add to OBJC:

B4X:
- (UIImage*) TakeScreenshot
    {
    UIWindow* window;
    if (@available (iOS 13, *))    { window = UIApplication.sharedApplication.windows.firstObject;  } else
        { window = UIApplication.sharedApplication.delegate.window; };
    UIGraphicsBeginImageContextWithOptions (window.bounds.size, NO, 0.0);
    [window.layer renderInContext: UIGraphicsGetCurrentContext ()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext (); // this captures the screenshot
    UIGraphicsEndImageContext ();
    return image;
    }

This function returns Bitmap. You can, for example, to assign it to Imageview:
B4X:
Dim no As NativeObject = Me
ImageView1.Bitmap = no.RunMethod ("TakeScreenshot", Null)

I tested under IOS 13 only, but should works in any 8+ also.
Yes, this code also works. ios 8+
But I think I was misunderstood because of the translation.
For example, on the screen pressing a button, will take a screenshot.

Our aim is to take another screenshot after the man takes a screenshot.

I am grateful to you for your help.
Is there a code example for this?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
As I understand, you want to replace previous screenshot. Not sure that it possible to do programmatically. At least, I did not saw samples.

In addition ... Screenshots is one problem. "Record Screen" (IOS built-in and separate programs) is another.
My conclusion - if user really wants, he will find a way to copy the screen, unlike all your efforts.
 
Upvote 0
Top