Skip to content

Commit

Permalink
fixes screenshot disabling in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
swciitg committed Aug 4, 2024
1 parent e857298 commit 150d4e5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,5 @@ windows/
.run/
.env
*.ps1
/appLoader.sh
/buildIPA.sh
93 changes: 52 additions & 41 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ 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]?
) -> Bool {
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
}
Expand All @@ -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
}
}

0 comments on commit 150d4e5

Please sign in to comment.