2using Plugin.Maui.ScreenSecurity.Handlers;
7 internal class ScreenshotProtectionManager
9 private static UITextField? _secureTextField =
null;
11 internal static void HandleScreenshotProtection(
bool enabled, UIWindow? window =
null)
15 SetScreenshotProtection(enabled, window);
19 ErrorsHandler.HandleException(nameof(HandleScreenshotProtection), ex);
23 private static void SetScreenshotProtection(
bool preventScreenshot, UIWindow? window)
25 DispatchQueue.MainQueue.DispatchAsync(() =>
31 _secureTextField ??=
new()
33 UserInteractionEnabled =
false
36 UIViewController? rootViewController = GetRootPresentedViewController(window);
38 rootViewController?.View?.AddSubview(_secureTextField);
39 window.MakeKeyAndVisible();
41 window.Layer.SuperLayer?.AddSublayer(_secureTextField.Layer);
43 _secureTextField.Layer.Sublayers?[0].AddSublayer(window.Layer);
46 if (_secureTextField !=
null)
48 if (preventScreenshot)
49 _secureTextField.SecureTextEntry = preventScreenshot;
52 _secureTextField.SecureTextEntry =
false;
53 _secureTextField =
null;
59 ErrorsHandler.HandleException(nameof(SetScreenshotProtection), ex);
64 private static UIViewController? GetRootPresentedViewController(UIWindow window)
66 UIViewController? viewController = window.RootViewController;
68 while (viewController?.PresentedViewController !=
null)
70 viewController = viewController.PresentedViewController;
73 return viewController;