Skip to content

Commit d6d1ae4

Browse files
Merge pull request #2 from TheCodeDaniel/fix/crash_issue
fix: crashing issues on IOS version
2 parents 2d131ad + 1ce5cb8 commit d6d1ae4

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class _MyAppState extends State<MyApp> {
2323
void initState() {
2424
super.initState();
2525
initPlatformState();
26+
_screenshotGuardPlugin.enableSecureFlag(enable: true);
2627
}
2728

2829
// Platform messages are asynchronous, so we initialize in an async method.

ios/Classes/ScreenshotGuardPlugin.swift

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,85 @@ import ScreenProtectorKit
55
public class ScreenshotGuardPlugin: NSObject, FlutterPlugin {
66

77
private var screenProtectorKit: ScreenProtectorKit?
8+
private var isScreenshotPreventionActive = false // Track the current state
9+
private var blackOverlay: UIView? // Black overlay for recent apps protection
810

911
public static func register(with registrar: FlutterPluginRegistrar) {
1012
let channel = FlutterMethodChannel(name: "screenshot_guard", binaryMessenger: registrar.messenger())
1113
let instance = ScreenshotGuardPlugin()
1214
registrar.addMethodCallDelegate(instance, channel: channel)
15+
registrar.addApplicationDelegate(instance) // Register lifecycle events
1316
}
1417

1518
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
1619
switch call.method {
1720
case "enableSecureFlag":
18-
if let args = call.arguments as? [String: Any], let enable = args["enable"] as? Bool {
19-
toggleScreenshotProtection(enable)
20-
result(nil)
21-
} else {
21+
guard let args = call.arguments as? [String: Any],
22+
let enable = args["enable"] as? Bool else {
2223
result(FlutterError(code: "INVALID_ARGUMENT", message: "Expected 'enable' boolean argument", details: nil))
24+
return
2325
}
26+
toggleScreenshotPrevention(enable)
27+
result(nil)
2428
case "getPlatformVersion":
25-
result("iOS " + UIDevice.current.systemVersion)
29+
result("iOS " + UIDevice.current.systemVersion)
2630
default:
2731
result(FlutterMethodNotImplemented)
2832
}
2933
}
3034

31-
private func toggleScreenshotProtection(_ enable: Bool) {
35+
private func toggleScreenshotPrevention(_ enable: Bool) {
36+
isScreenshotPreventionActive = enable
3237
if enable {
33-
// Initialize screenProtectorKit only when needed
38+
// Initialize and enable screenshot prevention
3439
if screenProtectorKit == nil {
35-
// Use the current window context to initialize ScreenProtectorKit
36-
screenProtectorKit = ScreenProtectorKit(window: UIApplication.shared.delegate?.window as? UIWindow)
40+
guard let window = UIApplication.shared.windows.first else {
41+
print("Failed to retrieve the main window")
42+
return
43+
}
44+
screenProtectorKit = ScreenProtectorKit(window: window)
45+
screenProtectorKit?.configurePreventionScreenshot()
3746
}
38-
screenProtectorKit?.configurePreventionScreenshot() // Configure prevention
39-
screenProtectorKit?.enabledPreventScreenshot() // Enable screenshot prevention
47+
screenProtectorKit?.enabledPreventScreenshot()
4048
} else {
41-
screenProtectorKit?.disablePreventScreenshot() // Disable screenshot protection
42-
screenProtectorKit = nil // Reset the screenProtectorKit instance
49+
// Disable screenshot prevention
50+
screenProtectorKit?.disablePreventScreenshot()
51+
}
52+
}
53+
54+
// MARK: - App Lifecycle Management
55+
56+
public func applicationWillResignActive(_ application: UIApplication) {
57+
// App is about to move to the background
58+
if isScreenshotPreventionActive {
59+
addBlackOverlay() // Protect app content in "Recent Apps"
60+
screenProtectorKit?.disablePreventScreenshot() // Avoid black screen
61+
}
62+
}
63+
64+
public func applicationDidBecomeActive(_ application: UIApplication) {
65+
// App has moved to the foreground
66+
if isScreenshotPreventionActive {
67+
removeBlackOverlay() // Restore visibility
68+
screenProtectorKit?.enabledPreventScreenshot() // Re-enable prevention
4369
}
4470
}
45-
}
4671

72+
// MARK: - Black Overlay for "Recent Apps"
73+
74+
private func addBlackOverlay() {
75+
guard let window = UIApplication.shared.windows.first else { return }
76+
if blackOverlay == nil {
77+
blackOverlay = UIView(frame: window.bounds)
78+
blackOverlay?.backgroundColor = UIColor.black
79+
blackOverlay?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
80+
}
81+
if let blackOverlay = blackOverlay, blackOverlay.superview == nil {
82+
window.addSubview(blackOverlay)
83+
}
84+
}
85+
86+
private func removeBlackOverlay() {
87+
blackOverlay?.removeFromSuperview()
88+
}
89+
}

0 commit comments

Comments
 (0)