Skip to content

Instantly share code, notes, and snippets.

@ashishkakkad8
Created July 18, 2022 17:12
Show Gist options
  • Select an option

  • Save ashishkakkad8/747223d68f3675dce4f05e4396baec7e to your computer and use it in GitHub Desktop.

Select an option

Save ashishkakkad8/747223d68f3675dce4f05e4396baec7e to your computer and use it in GitHub Desktop.
Prevent Screenshot of UIView - iOS
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)
}
}
}
@kyagie

kyagie commented Oct 20, 2022

Copy link
Copy Markdown

Please provide a demo to show how it works.
How can I implement this in react native?

@Nikooos

Nikooos commented Sep 1, 2023

Copy link
Copy Markdown

This no longer works in iOS17 (beta at least).

@ashishkakkad8

Copy link
Copy Markdown
Author

Please provide a demo to show how it works. How can I implement this in react native?

It's in iOS native code. So, I guess you have to do bridge for this.

@ashishkakkad8

Copy link
Copy Markdown
Author

This no longer works in iOS17 (beta at least).

Yes. I have also tested. Specifically above code is not working in iOS 17. I am also looking into it.

Meanwhile you can check following gist and screenshot prevention, which is totally working fine in iOS 17 too.

https://gist.github.com/ashishkakkad8/695e0b1828f699ed538bcf1a01920cc7

@Nikooos

Nikooos commented Sep 7, 2023

Copy link
Copy Markdown

Thanks for the new example. I will check it out.
Still curious what's the big difference here, but thanks again for looking into it!

@awnigharbia

Copy link
Copy Markdown

any solution for ios 17?

@awnigharbia

Copy link
Copy Markdown

@ashishkakkad8 Did you find any solution?

@ashishkakkad8

Copy link
Copy Markdown
Author

@awnigharbia As I have written above. I have already tested the https://gist.github.com/ashishkakkad8/695e0b1828f699ed538bcf1a01920cc7 code and it was working fine in iOS 17 beta. I didn't get time to check in latest versions. Once I get time sure I will check. Thanks :)

@awnigharbia

Copy link
Copy Markdown

Thanks I managed to solve it.

@ashishkakkad8

ashishkakkad8 commented Oct 2, 2023 via email

Copy link
Copy Markdown
Author

@yarodevuci

Copy link
Copy Markdown

@yarodevuci

Copy link
Copy Markdown

how to use this on table view???

@GManShen

Copy link
Copy Markdown

apple has warned not to use such methods of adding view's layer to another view's layer, this will break you UI. https://forums.developer.apple.com/forums/thread/722333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment