How to prevent screenshot for UIView in iOS?

iOS – Apple provides screenshot detection notification but not providing direct solution for prevention.

Here I found solution for preventing screenshot taking from the UIView. It’s a simple extension of the UIView that allows to hide it from screen-capturing and also from screen recording. The solution uses ability of UITextField to hide a password from capturing.

extension UIView {
func preventScreenshot() {
DispatchQueue.main.async {
let field = UITextField()
field.isSecureTextEntry = true
self.addSubview(field)
field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
self.layer.superlayer?.addSublayer(field.layer)
field.layer.sublayers?.first?.addSublayer(self.layer)
}
}
}

Usage:

viewSecure.preventScreenshot()

So, when you take screenshot the logic of text field secure entry (password text field) will not allow to take screenshot of that part.

ScrollView Screenshot Prevention

Update: Here is recursive function for preventing screenshot.

extension UIView {
func preventScrollViewScreenshotRecursive() {
guard superview != nil else {
for subview in subviews {
subview.preventScrollViewScreenshotRecursive()
}
return
}
let guardTextField = UITextField()
guardTextField.backgroundColor = .red
guardTextField.translatesAutoresizingMaskIntoConstraints = false
guardTextField.tag = Int.max
guardTextField.isSecureTextEntry = true
addSubview(guardTextField)
guardTextField.isUserInteractionEnabled = false
sendSubviewToBack(guardTextField)
layer.superlayer?.addSublayer(guardTextField.layer)
guardTextField.layer.sublayers?.first?.addSublayer(layer)
guardTextField.centerYAnchor.constraint(
equalTo: self.centerYAnchor
).isActive = true
guardTextField.centerXAnchor.constraint(
equalTo: self.centerXAnchor
).isActive = true
}
}

Support

If you like then Buy me a coffee ☕️

Conclusion

Let me know if you have any questions, comments, or feedback – contact me on Twitter.

Stay Safe At Home. Learn Something New. Share To The World.
Happy Coding 🙂

Did you know? How to hide keyboard in SwiftUI?

iOS 15 have new property wrapper: @FocusState. This is exactly like a regular @State property, except it’s specifically designed to handle input focus in our UI.

In this tutorial we will get to know that how to hide keyboard or you can say how to work with the @FocusState in SwiftUI.

Read more

WidgetKit: How to add deep link on iOS Widget?

Widget in iOS is massive update by Apple. Today we will discuss about the deep link on widget.

As per apple suggestions small widget have single tap target, medium and large widget can have multiple tap targets. For Example, a widget with multiple photos can have different tap targets to perform action on the specific photo.

Apple suggested to avoid offering so many targets that people have trouble tapping the one they want.

Let see how we can add it by code…

Read more

iOS 14 – How to use UIColorWell and UIColorPickerViewController?

iOS 14 comes up with three new concepts of pickers, which are Menu, Date Picker and Color Picker. All of this will be useful for the developers as well as users of iOS. Today we will learn about the UIColorWell and UIColorPickerViewController.

Read more

How to add new Firebase Crahlytics SDK to your iOS project?

Now a days Fabric is sending notice for migrating, as I got email today about [FINAL NOTICE] Fabric is shutting down May 4, 2020. We can move to the new Firebase Crashlytics SDK.

Integrating the new Firebase Crashlytics SDK is quick and easy. Let’s take a look.

Read more