skip to Main Content

I have a flutter app where I want to prevent users from taking screenshots and screen recordings. I have the following code which perfectly works for ios version lower than 17 but not for ios 17

AppDelegate

 override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    
    setUpBlurEffectView()
    
    NotificationCenter.default.addObserver(self, selector: #selector(toggleScreenBlurForCapture), name: UIScreen.capturedDidChangeNotification, object: nil)
   window?.makeSecure()
   return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}


func setUpBlurEffectView() {
        let blurEffect = UIBlurEffect(style: .regular)
        blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView?.frame = window?.bounds ?? UIScreen.main.bounds
        blurEffectView?.isHidden = true
        window?.addSubview(blurEffectView!)
    }


@objc func toggleScreenBlurForCapture() {
        let isCaptured = UIScreen.main.isCaptured
        blurEffectView?.isHidden = !isCaptured
    }

extension UIWindow {
    func makeSecure() {
        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)
    }
}

What am I doing wrong here? Has something changed on ios 17? Any hint or help would be appreciated.

2

Answers


  1. replace this

    extension UIWindow {
        func makeSecure() {
            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)
        }
    }
    

    to this

    extension UIWindow {
        func makeSecure() {
            let field = UITextField()
            let view = UIView(frame: CGRect(x: 0, y: 0, width: field.frame.self.width, height: field.frame.self.height))
            field.isSecureTextEntry = true
            self.addSubview(field)
            self.layer.superlayer?.addSublayer(field.layer)
            field.layer.sublayers?.last!.addSublayer(self.layer)
            field.leftView = view
            field.leftViewMode = .always
        }
    }
    
    Login or Signup to reply.
  2. use screen_protector package as it’s not possible to prevent the screenshotting on iOS but you can make the user screenshot a black screen instead of your content using the package i mentioned above.

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