How to check iOS11 Screen Recording is On or Off?
Asked Answered
K

6

10

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.

Kloman answered 15/9, 2017 at 6:2 Comment(2)
did u tried the mirrored property on UIScreen?Pepin
Thank you, Yes, I tried mirrored property, But I want to get screen recording events only.Kloman
M
13

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)
    }
}
Macri answered 4/3, 2018 at 21:22 Comment(8)
hi is there any simillar way to detect when user takes ScrrenShot.i know "UIApplicationUserDidTakeScreenshot" but it fired after user taken ScreenShot.Phlebitis
@Phlebitis any success with screenshot thing?Karate
@AnkitKumarGupta no bro still not found any solution.Phlebitis
But there is api screenshield that do the same thing.thay also have app:confindPhlebitis
Okk bro.. let me know if you have any update on this.Karate
Is this private?Cancer
@AlbertRenshaw noMacri
If Screen capturing is true, then I have to stop recording. Is it possible from my app? @MacriSteve
P
3

I guess you can alway check this variable regardless of the notification

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let isCaptured = UIScreen.main.isCaptured
    return true
}
Pepin answered 15/9, 2017 at 8:54 Comment(7)
Thank you for your response, But I will try to find the event's when user On or Off screen recording.Kloman
I also added the KVO in "captured" property. But it works only first time but when I kill the App and Launch again then not getting events from isCaptured. Is there any need for clear cache memory?Kloman
like i said you DO NOT need to track events or notification or "when it changed the captured value".. you can just read the value anytimePepin
Hi, yasirmturk in my project I want to know when user start screen recording, i.e my App is running and in between if user start screen recording in iOS11 then that time I want to know screen recording is On and take a particular action just like Netflix and Amazon Prime for preventing content from piracy.Kloman
this actually also detect when your phone is on AirPlay or connected to an external monitor... might not be the desired effect.Rally
I have to stop recording. Is it possible from my app? @PepinSteve
@Kloman did you get any solution for this?Corrosion
H
2

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;
}
Higher answered 30/1, 2019 at 17:49 Comment(0)
H
2

@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
    }
Hurrah answered 7/12, 2019 at 19:31 Comment(0)
S
0

this is the way to detect if a screenshot has been taken

func detectScreenShot(action: @escaping () -> ()) {
    let mainQueue = OperationQueue.main
    NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
        // executes after screenshot
        action()
    }
}
Stocks answered 27/1, 2019 at 16:13 Comment(0)
H
0

Using combine, you can subscribe to the isCaptured property of UIScreen and it will give you both the capturing and non-capturing states when the user starts and completes recording:

private var cancellables: [AnyCancellable] = []

UIScreen.main.publisher(for: \.isCaptured)
   .sink { isCaptured in
       // Your code
   }
   .store(in: &cancellables)
Honor answered 14/2, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.