From e857298f10053b248c714ac701e2fd269aed7ea8 Mon Sep 17 00:00:00 2001 From: Hardik Roongta Date: Sun, 4 Aug 2024 12:35:13 +0530 Subject: [PATCH] changes in AppDelegate for disabling screenshots --- ios/Runner/AppDelegate.swift | 41 ++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 1e56f81b..564c5ad8 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -21,7 +21,7 @@ import flutter_local_notifications result(FlutterMethodNotImplemented) return } - self?.preventScreenshots(arguments: call.arguments as? Bool ?? false) + self?.preventScreenshots(enabled: call.arguments as? Bool ?? false) result(nil) }) if #available(iOS 10.0, *) { @@ -41,16 +41,39 @@ import flutter_local_notifications GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } - private func preventScreenshots(arguments: Bool) { - if arguments { - let snapshotView = UIView(frame: UIScreen.main.bounds) - snapshotView.backgroundColor = UIColor.white // You can set any color you like - window?.addSubview(snapshotView) - self.snapshotView = snapshotView + private func preventScreenshots(enabled: Bool) { + if enabled { + NotificationCenter.default.removeObserver( + self, + name: UIApplication.userDidTakeScreenshotNotification, + object: nil + ) } else { - self.snapshotView?.removeFromSuperview() - self.snapshotView = nil + NotificationCenter.default.addObserver( + self, + selector: #selector(didTakeScreenshot), + name: UIApplication.userDidTakeScreenshotNotification, + object: nil + ) } } + + @objc private func didTakeScreenshot() { + guard let window = self.window else { return } + // Add a blur effect view to hide sensitive information + let blurEffect = UIBlurEffect(style: .light) + let blurEffectView = UIVisualEffectView(effect: blurEffect) + blurEffectView.frame = window.bounds + window.addSubview(blurEffectView) + + // Store the blur effect view in a property so it can be removed later + self.blurEffectView = blurEffectView + + // Optionally remove the blur effect after some delay + DispatchQueue.main.asyncAfter(deadline: .now() + 3) { + blurEffectView.removeFromSuperview() + self.blurEffectView = nil + } + } }