How to disable screen recording in iOS app programmatically

Published November 20, 2020

In this post, we will learn How to disable Screen Recording in iOS application programmatically.

 

  • Create an ios Application
  • Add object C files to the created project.
  • By using the  notification add observer methods we can trigger the screen recording status

 

ScreenRecordingDetector.h

//
//  ScreenRecordingDetector.h
//  ScreenCaptureDetector

/*
ScreenRecordingDetector checks for screen capturing as well as airplay mirroring
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern NSString *kScreenRecordingDetectorRecordingStatusChangedNotification;

@interface ScreenRecordingDetector : NSObject

+(instancetype)sharedInstance;
+ (void)triggerDetectorTimer;
+ (void)stopDetectorTimer;
- (BOOL)isRecording;

@end
 

 

 

ScreenRecordingDetector.m

//
//  ScreenRecordingDetector.m
//  ScreenCaptureDetector

#import "ScreenRecordingDetector.h"
float const kScreenRecordingDetectorTimerInterval = 1.0;
NSString *kScreenRecordingDetectorRecordingStatusChangedNotification = @"kScreenRecordingDetectorRecordingStatusChangedNotification";

@interface ScreenRecordingDetector()

@property BOOL lastRecordingState;
@property NSTimer *timer;

@end
@implementation ScreenRecordingDetector


+ (instancetype)sharedInstance {
    static ScreenRecordingDetector *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
- (id)init {
    if (self = [super init]) {
        // do some init stuff here..
        self.lastRecordingState = NO; // initially the recording state is 'NO'. This is the default state.
        self.timer = NULL;
    }
    return self;
}
- (BOOL)isRecording {
    for (UIScreen *screen in UIScreen.screens) {
        if ([screen respondsToSelector:@selector(isCaptured)]) {
            // iOS 11+ has isCaptured method.
            if ([screen performSelector:@selector(isCaptured)]) {
                return YES; // screen capture is active
            } else if (screen.mirroredScreen) {
                return YES; // mirroring is active
            }
        } else {
            // iOS version below 11.0
            if (screen.mirroredScreen)
                return YES;
        }
    }
    return NO;
}
+ (void)triggerDetectorTimer {

    ScreenRecordingDetector *detector = [ScreenRecordingDetector sharedInstance];
    if (detector.timer) {
        [self stopDetectorTimer];
    }
    detector.timer = [NSTimer scheduledTimerWithTimeInterval:kScreenRecordingDetectorTimerInterval
                                                           target:detector
                                                         selector:@selector(checkCurrentRecordingStatus:)
                                                         userInfo:nil
                                                          repeats:YES];
}
- (void)checkCurrentRecordingStatus:(NSTimer *)timer {
    BOOL isRecording = [self isRecording];
    if (isRecording != self.lastRecordingState) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName: kScreenRecordingDetectorRecordingStatusChangedNotification object:nil];
    }
    self.lastRecordingState = isRecording;
}
+ (void)stopDetectorTimer {
    ScreenRecordingDetector *detector = [ScreenRecordingDetector sharedInstance];
    if (detector.timer) {
        [detector.timer invalidate];
        detector.timer = NULL;
    }
}
@end

 

 

NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name:UIApplication.didBecomeActiveNotification, object: nil)

              

              NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name:UIApplication.willResignActiveNotification, object: nil)

  

 

      NotificationCenter.default.addObserver(self, selector: #selector(screenCaptureStatusChanged), name: NSNotification.Name.screenRecordingDetectorRecordingStatusChanged, object: nil)

 

     @objc func appDidBecomeActive (){

         ScreenRecordingDetector.triggerDetectorTimer()

         print("become active")

         

         if (ScreenRecordingDetector.sharedInstance().isRecording())

         {

            

 

             print("Recording started")

             

         }

         }

     @objc func applicationWillResignActive () {

         ScreenRecordingDetector.stopTimer()

       

         

         print("Resign active")

     }

   

     

  @objc func screenCaptureStatusChanged () {

         setupView()

         

         

         print("screencaptured active")

 

     }

    //MARK:Recording SetUp

    

    func setupView() {

        

        if (ScreenRecordingDetector.sharedInstance().isRecording())

        {

            

            print("Recording started")

            

        }

        

        if (ScreenRecordingDetector.sharedInstance().isRecording() == true)

        {

            print("Recording already exists")

            

        }

            

        else

        {

            print("Recording not started")

        }

          }

 

 

Find Example code at https://github.com/rrtutors/ScreenRecording-ios

 

 

How to take a screenshot in Ios Programmatically

 

How to Make a Phone call in Ios-10 Using Swift

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2345 Views