diff --git a/.gitignore b/.gitignore index 3e0ad20..0e1446a 100644 --- a/.gitignore +++ b/.gitignore @@ -189,3 +189,5 @@ windows/ .run/ .env *.ps1 +/appLoader.sh +/buildIPA.sh diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 564c5ad..222eead 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -6,8 +6,8 @@ import flutter_local_notifications @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { + private var blurEffectView: UIVisualEffectView? private let SCREENSHOT_CHANNEL = "com.example.app/screenshot" - private var snapshotView: UIView? override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? @@ -15,15 +15,15 @@ import flutter_local_notifications let controller : FlutterViewController = window?.rootViewController as! FlutterViewController let screenshotChannel = FlutterMethodChannel(name: SCREENSHOT_CHANNEL, binaryMessenger: controller.binaryMessenger) - screenshotChannel.setMethodCallHandler({ - [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in - guard call.method == "preventScreenshots" else { - result(FlutterMethodNotImplemented) - return - } - self?.preventScreenshots(enabled: call.arguments as? Bool ?? false) - result(nil) - }) + screenshotChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in + if call.method == "preventScreenshots", let secure = call.arguments as? Bool { + self.setSecureScreen(secure) + result(nil) + } else { + result(FlutterMethodNotImplemented) + } + } + if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate } @@ -39,41 +39,52 @@ import flutter_local_notifications } GMSServices.provideAPIKey(dartDefinesDictionary["GMAP_KEY"] as? String ?? "") GeneratedPluginRegistrant.register(with: self) + return super.application(application, didFinishLaunchingWithOptions: launchOptions) } - private func preventScreenshots(enabled: Bool) { - if enabled { - NotificationCenter.default.removeObserver( - self, - name: UIApplication.userDidTakeScreenshotNotification, - object: nil - ) - } else { - 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) + @objc private func userDidTakeScreenshot() { + if let window = UIApplication.shared.keyWindow { + addBlurEffect(to: window) + DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { + self.removeBlurEffect() + } + } + } - // Store the blur effect view in a property so it can be removed later - self.blurEffectView = blurEffectView + private func setSecureScreen(_ secure: Bool) { + if secure { + NotificationCenter.default.addObserver(self, selector: #selector(handleApplicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleApplicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil) + } else { + NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil) + NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil) + } + } - // Optionally remove the blur effect after some delay - DispatchQueue.main.asyncAfter(deadline: .now() + 3) { - blurEffectView.removeFromSuperview() - self.blurEffectView = nil - } - } + @objc private func handleApplicationWillResignActive(_ notification: Notification) { + if let window = UIApplication.shared.keyWindow { + addBlurEffect(to: window) + } + } + + @objc private func handleApplicationDidBecomeActive(_ notification: Notification) { + removeBlurEffect() + } + + private func addBlurEffect(to window: UIWindow) { + let blurEffect = UIBlurEffect(style: .dark) + let blurEffectView = UIVisualEffectView(effect: blurEffect) + blurEffectView.frame = window.bounds + blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + blurEffectView.tag = 999 + window.addSubview(blurEffectView) + self.blurEffectView = blurEffectView + } + + private func removeBlurEffect() { + blurEffectView?.removeFromSuperview() + blurEffectView = nil + } }