skip to Main Content

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

FYI: Link used for the above approach

2

Answers


  1. 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:

    func disableSecure() {
        DispatchQueue.main.async {
            self.view.subviews.first(where: { $0 is UITextField })?.removeFromSuperview()
        }
    }
    

    This code will find the first UITextField subview of the view and remove it from the view.

    Here is the complete code:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let makeSecureButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
            makeSecureButton.center = view.center
            makeSecureButton.setTitle("Make Secure", for: .normal)
            makeSecureButton.addTarget(self, action: #selector(makeSecure), for: .touchUpInside)
            view.addSubview(makeSecureButton)
    
            let disableSecureButton = UIButton(frame: CGRect(x: 0, y: 50, width: 200, height: 50))
            disableSecureButton.center = view.center
            disableSecureButton.setTitle("Disable Secure", for: .normal)
            disableSecureButton.addTarget(self, action: #selector(disableSecure), for: .touchUpInside)
            view.addSubview(disableSecureButton)
        }
    
        @objc func makeSecure() {
            DispatchQueue.main.async {
                let field = UITextField()
                field.isSecureTextEntry = true
                self.view.addSubview(field)
                field.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
                field.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
                self.view.layer.superlayer?.addSublayer(field.layer)
                field.layer.sublayers?.first?.addSublayer(self.view.layer)
            }
        }
        
        @objc func disableSecure() {
            DispatchQueue.main.async {
                self.view.subviews.first(where: { $0 is UITextField })?.removeFromSuperview()
            }
        }
    }
    

    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.

    Login or Signup to reply.
  2. The ‘isSecureTextEntry’ way looks not working on iOS 17 now. Looking for other solutions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search