SoFunction
Updated on 2025-04-07

Implementation code for iOS screen recording and screenshot monitoring

Recently, I have been working on project security and need to prevent user screenshots and recording on sensitive pages in the APP. I checked some information online and took a note here to facilitate future search.

Screenshot status acquisition

The method to edit the latest photos in the album has expired after iOS8, and the framework "Photos" has also expired after iOS10.

The search found that UIApplication only has notifications after the user's screenshot, and the application will only receive notifications that have been screenshot and cannot interfere.

// 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 it is impossible to directly intervene, you can know that the user can use other methods to restrict the user's behavior or pop up a prompt to tell the user.

-(void)viewDidAppear:(BOOL)animated{
  [super viewDidAppear:animated];
  
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

-(void)screenshots
{
  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[Safety Reminder] Includes personal fund accounts. Do not take screenshots, record or share them with others to ensure the security of the fund accounts." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Sure", nil];
  [alert1 show];

-(void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

Retrieve screen recording status

The iOS 11 SDK has added a UIScreen API to inform the application that the current screen is recording. When true, it means that the current screen is being recorded, mirrored, or sent by Airplay.

When the recording state changes, UIKit will send the notification of UIScreenCapturedDidChange.

Based on this, we can receive this notification in the application to handle the user's screen recording behavior accordingly.

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];

//Monitor whether the current device is in the screen recording state  UIScreen * sc = [UIScreen mainScreen];
  if (@available(iOS 11.0, *)) {
    if () {
      NSLog(@"Recording~~~~~~~%d",);
      [self screenshots];
    }
  } else {
    // Fallback on earlier versions
  }
  if (@available(iOS 11.0, *)) {
//The current device screen recording status has been detected to change.    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIScreenCapturedDidChangeNotification object:nil];
  } else {
    // Fallback on earlier versions
  }
}

-(void) screenshots
{
  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[Safety Reminder] Includes personal fund accounts. Do not take screenshots, record or share them with others to ensure the security of the fund accounts." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Sure", nil];
  [alert1 show];

-(void)dealloc
{
  if (@available(iOS 11.0, *)) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenCapturedDidChangeNotification object:nil];
  } else {
    // Fallback on earlier versions
  }
}

The above-mentioned monitoring of screen recording status is only after iOS11, and it is only detected that the screen recording status is not possible or to modify the recorded content. As for the monitoring of screen recording methods before iOS11, the screen recording method has not been found yet. If anyone knows it, please let me know. Thank you.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.