How to stop users taking screenshots or recordings of your iOS app
Guard your app content from the screenshot and recording is not an easy one so far in iOS, this workaround would help us in the time being until we get an official API from apple.
Let’s assume that you as an iOS developer are working on an application where we have some secure/sensitive contents like mobile number/bank details, etc. One of the security concerns is, view/view controller will get captured when someone press(home+lock button in old iPhones, volume up + lock button in newer iOS devices). It’s been a challenging one for iOS developers to protect the app content from the screenshots. In this article, let’s see how we can overcome this problem.
Using Notification to prevent the screen from screen capture:
Apple provides “userDidTakeScreenshotNotification” notification, but that notifies us only after the screenshot is taken. So this is not something we wanted.
ScreenshieldKIT:
“ScreenshieldKit” works the way we want, but it is a licensed and we have to pay for it. So not all developers/business can make this. And we have to use their simple UI component. Believe me, i googled a lot to understand how ScreenshieldKit works, but still its a wild guess only, some people says that screenshileldKit uses DRM video overlay pattern, i am not convinced though. But still my hungry towards achieving it was still alive. Hence i tried various things to protect our iOS app content from Screenshots.
So, What are the alternatives i tried to protect the app views from screen capture and recording?
I have tried a couple of things to make it work as ScreenshieldKit by doing much surfing in stack-overflow and various forums..
- Deleting the photo from the library after screenshot, aha thats bad, user permission is needed and not the good one, so this is not an option at all :x:
- Listening for the overlay which is appearing when the user is about to taking a screenshot. This seemed an option, but window change also comes after screenshot is taken. And i am not able to detect before the screenshot.
- Observing isCaptured property, hmm thats working for mirroring/recording.
Working solution to secure the app screens from iOS screenshot and mirroring?
Have you ever noticed Secure Textfield Entry when the screenshot is being taken.. if not please have a look at this video.
https://www.youtube.com/watch?v=YJKqtv8aj_w
So, the idea is derived from there and thanks to a stackoverflow post to trigger this.
what we do here, we add our window or UIView into the UITextField’s layer (which enables secure entry), then that’s it rest of the things will be handled by the OS itself. Below is the little code snippet for that as an extension of UIView. By doing this way, we can protect the content from screenshot, recording and also from mirroring.
private extension UIView {func setScreenCaptureProtection() {guard superview != nil else {for subview in subviews { //to avoid layer cyclic crash, when it is a topmost view, adding all its subviews in textfield's layer, TODO: Find a better logic.subview.setScreenCaptureProtection()}return}let guardTextField = UITextField()guardTextField.backgroundColor = .redguardTextField.translatesAutoresizingMaskIntoConstraints = falseguardTextField.isSecureTextEntry = trueaddSubview(guardTextField)guardTextField.isUserInteractionEnabled = falsesendSubviewToBack(guardTextField)layer.superlayer?.addSublayer(guardTextField.layer)guardTextField.layer.sublayers?.first?.addSublayer(layer)guardTextField.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = trueguardTextField.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = trueguardTextField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = trueguardTextField.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true}}
For mirroring and recording, Apple provides a proper, elegant solution by observing isCaptured property of UIScreen.
Interested for full source code which has the ability to protect a particular view from screenshot or a complete window?. Complete source code is here and this one is also published as cocoapod, you can simply plugin in your project.
One remark: There might be some issues with view hierarchy, I have some to-do’s to work on it. I would spend time based on the response I get for that library. Thanks!!!. Happy coding!!!.
Hope this little workaround might help you for time being till we get an easy API from Apple. I would have to go back to workspace to think about the same logic for SwiftUI apps.
You might be interested on My other articles such as Swift Actors, MainActor, Thread Sanitizer and etc.
Upgrade your medium membership from here to read good amount of article and scale your skills.