I am able to disable screen capture in iOS using this code
Objective- C
- (void)makeSecure {
dispatch_async(dispatch_get_main_queue(), ^{
UITextField *field = [[UITextField alloc] init];
field.secureTextEntry = true;
[self addSubview:field];
[[field.centerYAnchor constraintEqualToAnchor:self.centerYAnchor] setActive:YES];
[[field.centerXAnchor constraintEqualToAnchor:self.centerXAnchor] setActive:YES];
[self.layer.superlayer addSublayer:field.layer];
[field.layer.sublayers.firstObject addSublayer:self.layer];
});
}
Swift
func makeSecure() {
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)
}
}
But I am unable to enable the screen capture again.
So is there any way to enable and disable the above code
2
Answers
The code you shared is to disable screen capture by adding a UITextField with
isSecureTextEntry
property set to true to the view. This will make the text field appear as a secure text field, and the user will not be able to take a screenshot of the text field.To enable screen capture again, you can remove the UITextField from the view. You can do this by adding the following code to your view controller:
This code will find the first UITextField subview of the view and remove it from the view.
Here is the complete code:
This code will create two buttons, one to make the screen secure and one to disable the security. When the user clicks the "Make Secure" button, a UITextField will be added to the view and the screen will be locked. When the user clicks the "Disable Secure" button, the UITextField will be removed from the view and the screen will be unlocked.
Please note that this code is just a simple example and there are other ways to disable screen capture in iOS. The best way to disable screen capture will depend on your specific needs.
The ‘isSecureTextEntry’ way looks not working on iOS 17 now. Looking for other solutions