Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScreenshotProtectionManager.cs
1using CoreFoundation;
2using Plugin.Maui.ScreenSecurity.Handlers;
3using UIKit;
4
6{
7 internal class ScreenshotProtectionManager
8 {
9 private static UITextField? _secureTextField = null;
10
11 internal static void HandleScreenshotProtection(bool enabled, UIWindow? window = null)
12 {
13 try
14 {
15 SetScreenshotProtection(enabled, window);
16 }
17 catch (Exception ex)
18 {
19 ErrorsHandler.HandleException(nameof(HandleScreenshotProtection), ex);
20 }
21 }
22
23 private static void SetScreenshotProtection(bool preventScreenshot, UIWindow? window)
24 {
25 DispatchQueue.MainQueue.DispatchAsync(() =>
26 {
27 try
28 {
29 if (window != null)
30 {
31 _secureTextField ??= new()
32 {
33 UserInteractionEnabled = false
34 };
35
36 UIViewController? rootViewController = GetRootPresentedViewController(window);
37
38 rootViewController?.View?.AddSubview(_secureTextField);
39 window.MakeKeyAndVisible();
40
41 window.Layer.SuperLayer?.AddSublayer(_secureTextField.Layer);
42
43 _secureTextField.Layer.Sublayers?[0].AddSublayer(window.Layer);
44 }
45
46 if (_secureTextField != null)
47 {
48 if (preventScreenshot)
49 _secureTextField.SecureTextEntry = preventScreenshot;
50 else
51 {
52 _secureTextField.SecureTextEntry = false;
53 _secureTextField = null;
54 }
55 }
56 }
57 catch (Exception ex)
58 {
59 ErrorsHandler.HandleException(nameof(SetScreenshotProtection), ex);
60 }
61 });
62 }
63
64 private static UIViewController? GetRootPresentedViewController(UIWindow window)
65 {
66 UIViewController? viewController = window.RootViewController;
67
68 while (viewController?.PresentedViewController != null)
69 {
70 viewController = viewController.PresentedViewController;
71 }
72
73 return viewController;
74 }
75 }
76}