Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check iOS11 Screen Recording is On or Off?

Tags:

ios

swift

ios11

For detecting iOS11 screen recording feature On or Off I used isCaptured and UIScreenCapturedDidChange Notification.

When first time I Launch the App and On iOS11 built-in screen recording feature then it notifies the selector method with value True, but when I kill (terminate) my running App and Launch app again do the same procedure again then my selector method is not getting called.

Here is my code:

I add an Observer in ViewWillAppear() method:

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

The selector method is as follows:

@objc
func handleNotification(notification:Notification){

    let isCaptured = UIScreen.main.isCaptured

    print("isCaptured value = \(isCaptured)")
}

In this case, I need to kill the app, clear the cache and again launch the app for getting screen recording event.

Please suggest what I can do here to detect recording event to protect my content from recording.

like image 420
UmeshKumath Avatar asked Sep 15 '17 06:09

UmeshKumath


3 Answers

Swift 4

Add Observer

UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)

Receive changes

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured

        print(isCaptured)
    }
}
like image 108
dimohamdy Avatar answered Oct 23 '22 14:10

dimohamdy


@UmeshKumath this is easy when kill the app clear the cache and again launch the app for getting screen recording event you need to run the code in viewdidload just like so:

override func viewDidLoad() {
    super.viewDidLoad()
UIScreen.main.addObserver(self, forKeyPath: "some key", options: .new, context: nil)
    let isCaptured = UIScreen.main.isCaptured
    if isCaptured == true {
        // do something
    }
like image 25
Monger Avatar answered Oct 23 '22 14:10

Monger


The feature is available on and above iOS11. Better keep it inside didFinishLaunchingWithOptions

Objective-C syntax

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: 
 (NSDictionary *)launchOptions
{
if (@available(iOS 11.0, *)) {
    BOOL isCaptured = [[UIScreen mainScreen] isCaptured];
    if(isCaptured){
       // Do the action for hiding the screen recording
                  } 
   } else{
        // Fallback on earlier versions
         }
 return YES;
}
like image 31
Ankit Aman Avatar answered Oct 23 '22 13:10

Ankit Aman