1

I am trying to check if the screen is recording before allowing a following action, I initially tried to use ReplayKit to automatically record, but this isn't a viable solution because it doesn't allow for recording outside the app, so basically what I want to do is check if the user has began screen recording using the IOS Control Centre recorder, before allowing them to execute another piece of code.

Something like:

func handleScreen() {
    var isRecording: Bool = false

    if ScreenIsRecording { //(this is what i don't know how to check) 
       isRecording = true 
    }

    if isRecording == true {
     //   execute this code.
    }
            
}

I am open to other solutions of being able to execute screen recording, but it has to be able to record all screens not just the in-app screen.

Thanks

Anthony
  • 929
  • 7
  • 21

2 Answers2

6

UIScreen includes the UIScreen.isCaptured property which you should be able to reference to determine if the screen is likely being recorded. However this will also return true if the device is being AirPlayed or otherwise broadcast:

A value of YES indicates the system is actively recording, mirroring, or using AirPlay to stream the contents of the screen.

if UIScreen.mainScreen().isCaptured {
    isRecording = true
}
esqew
  • 42,425
  • 27
  • 92
  • 132
  • the proper variable name would be isRecordingMirroringOrAirPlaying. Here, I fixed it for you :) – Dercsár Aug 10 '20 at 13:28
  • Brilliant. Although I had to do `UIScreen.main.isCaptured` rather than `mainScreen()` I guess cause it was inside scope? One more thing, how can I prompt a user to end screen recording after the executed code has completed? – Anthony Aug 10 '20 at 13:34
  • You might just have to stick with normal methods of alerting - `UIAlertController` and the like... if your users aren't using `ReplayKit` you won't have much of a say when they stop a system-wide screen recording. – esqew Aug 10 '20 at 13:45
  • is there an appkit equivalent? – Akash Kundu Aug 28 '21 at 20:55
1

I just tested this on iOS 16, and even though the documentation says that isCaptured should return true for AirPlay. It does not when I tested it!

Observe changes to isCaptured with NotificationCenter:

NotificationCenter.default.addObserver(self, selector: #selector(screenCaptureDidChange),
                                       name: UIScreen.capturedDidChangeNotification,
                                       object: nil)

Handle the notification:

@objc func screenCaptureDidChange() {
    debugPrint("screenCaptureDidChange.. isCapturing: \(UIScreen.main.isCaptured)")
    
    if !UIScreen.main.isCaptured {
        //TODO: They stopped capturing..
    } else {
        //TODO: They started capturing..
        debugPrint("screenCaptureDidChange - is recording screen")
    }
}

This notification does NOT get fired when you start AirPlay, and if you start screen recording while AirPlaying, when you stop recording the notification will get fired but UIScreen.main.isCaptured is false even though AirPlay is still active.

Mikkel Cortnum
  • 481
  • 4
  • 11