In this post, we will learn How to disable Screen Recording in iOS application programmatically. ScreenRecordingDetector.h // /* #import <Foundation/Foundation.h> extern NSString *kScreenRecordingDetectorRecordingStatusChangedNotification; @interface ScreenRecordingDetector : NSObject +(instancetype)sharedInstance; @end ScreenRecordingDetector.m // #import "ScreenRecordingDetector.h" @interface ScreenRecordingDetector() @property BOOL lastRecordingState; @end ScreenRecordingDetector *detector = [ScreenRecordingDetector sharedInstance]; 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
// ScreenRecordingDetector.h
// ScreenCaptureDetector
ScreenRecordingDetector checks for screen capturing as well as airplay mirroring
*/
#import <UIKit/UIKit.h>
+ (void)triggerDetectorTimer;
+ (void)stopDetectorTimer;
- (BOOL)isRecording;
// ScreenRecordingDetector.m
// ScreenCaptureDetector
float const kScreenRecordingDetectorTimerInterval = 1.0;
NSString *kScreenRecordingDetectorRecordingStatusChangedNotification = @"kScreenRecordingDetectorRecordingStatusChangedNotification";
@property NSTimer *timer;
@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 {
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
Article Contributed By :
|
|
|
|
1916 Views |