[iOS] prevent users from screenshots

[iOS] prevent users from screenshots

Yesterday, the product asked me for new requirements, saying that the boss thought the information in the app was very important, and asked to add restrictions on users' screenshots.
At that time, I refused. I told him that this could not be realized. The screenshot of Home+Power was that the system operation could not be interfered in the application. He didn't trust him enough to call Apple's customer service, who replied that he had seen an application before that was to replace the screenshot with a white or black image.

IOS 11.2.1 and Xcode Version 9.2 are currently used.
The method of editing the latest Photos in the album iOS8 has become invalid, and the frame "Photos" has also become invalid after iOS10.

Search found that only the user's screenshot notification is available in UIApplication, and only the screenshot notification will be received in the application, and there is no way to intervene.
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
UIKIT_EXTERN NSNotificationName const UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);
Although we can't directly intervene, we can know that users can limit their behavior in other ways after screenshots.
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    return YES;

// Method of notification response after Home+Power key screen capture
- (void)userDidTakeScreenshotNotification:(NSNotification *)notification {
    // The following image is not the same as the hardware screenshot. Here, the code screenshot is used again to get the screenshot of the user.
    UIImage *image = [self imageWithScreenshot];
     If the APP has high confidentiality requirements, the image can be encoded and uploaded to the server, so as to provide a tracing method when necessary.
     If it's an APP of its own, it's a bit more aggressive. The screenshot behavior of the user is monitored here. It's OK for the application to directly force the user to log out and seal.

// Code screenshot
- (UIImage *)imageWithScreenshot {
    CGSize imageSize = CGSizeZero;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    UIImage *screenImage = [UIImage imageWithData:imageData];
    return screenImage;


When I searched before, I saw a Post:
It can be realized through Configuration Profile
allowScreenShot (Boolean)
Optional. When false, users are unable to save a screenshot of the display.

I understand. I don't quite understand.

Configuration Profiles

Xcode - multiple configurations with Configuration

Keywords: iOS xcode Windows

Added by AShain on Sat, 02 May 2020 15:07:36 +0300