Plugin.Maui.ScreenSecurity提供了一种无缝的解决方案,以防止内容曝光,并阻止.NET MAUI应用程序中的屏幕截图和录音
| 平台 | 版本 |
|---|---|
| .NET MAUI ANDROID | API 21+ |
| .net毛伊ios | iOS 14+ |
| 视窗 | 10.0.17763+ |
IsProtectionEnabled属性,以检查是否已经启用或禁用屏幕保护。ScreenCaptured事件处理程序,该事件处理程序会触发通知,当拍摄屏幕截图或记录屏幕时。Blazor样品以展示此插件的实现。单击此处查看完整的ChangElog!
Plugin.Maui.ScreenSecurity可以通过Nuget获得,抓住最新的软件包并将其安装在您的解决方案上:
Install-Package Plugin.Maui.ScreenSecurity
初始化您的MauiProgram类中的插件:
using Plugin . Maui . ScreenSecurity ;
public static MauiApp CreateMauiApp ( )
{
var builder = MauiApp . CreateBuilder ( ) ;
builder
. UseMauiApp < App > ( )
. ConfigureFonts ( fonts =>
{
fonts . AddFont ( "OpenSans-Regular.ttf" , "OpenSansRegular" ) ;
fonts . AddFont ( "OpenSans-Semibold.ttf" , "OpenSansSemibold" ) ;
} )
. UseScreenSecurity ( ) ;
return builder . Build ( ) ;
}在您的Android.manifest文件(平台/android)中添加以下权限:
< uses-permission android : name = " android.permission.DETECT_SCREEN_CAPTURE " />最后,将插件的默认实例添加为单例将其注入您的代码中:
builder . Services . AddSingleton < IScreenSecurity > ( ScreenSecurity . Default ) ; 重要的是要认识到,防止用户拍摄屏幕截图或应用程序的录音可能是一项具有挑战性的任务,并且实现完全预防可能是不可行的。值得注意的是,没有任何方法可以完全消除您的屏幕通过另一个物理设备或OS中潜在漏洞捕获的可能性。
在实施这些方法中的任何一种并与应用程序的安全问题达到平衡时,考虑对用户体验的影响也很重要。
如果您仍在使用1.0.0版本,请参考以前版本的旧文档。
新的统一API包括两种方法: ActivateScreenSecurityProtection()和DeactivateScreenSecurityProtection() ,可选参数仅适用于iOS平台。它还提供了两个属性: IsProtectionEnabled ,检查保护是否处于活动状态,并且ScreenCaptured事件处理程序,这些事件处理程序会通知何时拍摄屏幕截图或记录屏幕。
void ActivateScreenSecurityProtection ( ) ;当您激活此保护时,将应用程序的内容发送到Recents屏幕或App Switcher时将得到保护。这有助于确保不会暴露敏感信息。
void ActivateScreenSecurityProtection ( bool blurScreenProtection , bool preventScreenshot , bool preventScreenRecording ) ;此方法类似于以前的方法,但是具有更改iOS中默认值的参数:
blurScreenProtection :启用/禁用屏幕模糊以防止背景中的内容可见性。默认情况下为true 。preventScreenshot :确定用户是否可以获取应用程序的屏幕截图。默认情况下为true 。preventScreenRecording :控制用户在使用应用程序时是否可以记录屏幕。默认情况下为true 。 void ActivateScreenSecurityProtection ( ScreenProtectionOptions screenProtectionOptions ) ;此方法类似于原始方法,但采用ScreenProtectionOptions参数。这使您可以通过指定颜色或图像以及iOS设备的屏幕截图和屏幕记录保护来进一步自定义屏幕保护。
注意:如果同时设置颜色和图像,它将仅应用您先声明的颜色和图像。
ScreenProtectionOptions属性:
Color :代表十六进制字符串形式的颜色,可以作为参数传递以自定义颜色层。它支持#RGB , #RGBA , #RRGGBB或#RRGGBBAA等格式。默认情况下为空。Image :图像文件的名称及其扩展名。为了利用此属性,请遵循以下步骤:ResourcesImages文件夹中使用的图像。MauiImage之后无法识别图像,请考虑将构建操作更改为Embedded resource以确保正确的功能。PreventScreenshot :确定用户是否可以获取应用程序的屏幕截图。默认情况下为true 。PreventScreenRecording :控制用户在使用应用程序时是否可以记录屏幕。默认情况下为true 。 void DeactivateScreenSecurityProtection ( ) ;此方法停用了所有屏幕安全保护。
bool IsProtectionEnabled { get ; }此Bool检查是否启用了屏幕保护。
event EventHandler < EventArgs > ? ScreenCaptured ;当屏幕捕获屏幕时,通过屏幕快照或在Android和iOS上录制屏幕时会触发事件处理程序,但仅用于Windows上的屏幕截图。
public partial class MainPage : ContentPage
{
private readonly IScreenSecurity _screenSecurity ;
public MainPage ( IScreenSecurity screenSecurity )
{
InitializeComponent ( ) ;
_screenSecurity = screenSecurity ;
}
protected override void OnAppearing ( )
{
base . OnAppearing ( ) ;
// Check if screen security protection is not enabled
if ( ! _screenSecurity . IsProtectionEnabled )
{
// Activate the screen security protection with default settings
_screenSecurity . ActivateScreenSecurityProtection ( ) ;
}
// Attach to the ScreenCaptured event handler
_screenSecurity . ScreenCaptured += OnScreenCaptured ;
/*
// For changing iOS options, follow one of the next examples:
// Example 1: Customize with a specific color
var screenProtectionOptions = new ScreenProtectionOptions
{
HexColor = "#6C4675",
PreventScreenshot = true,
PreventScreenRecording = false
};
// Example 2: Customize with an image
var screenProtectionOptions = new ScreenProtectionOptions
{
Image = "protection_bg.png"
PreventScreenshot = false,
PreventScreenRecording = true
};
_screenSecurity.ActivateScreenSecurityProtection(screenProtectionOptions);
*/
}
protected override void OnDisappearing ( )
{
_screenSecurity . DeactivateScreenSecurityProtection ( ) ;
// Detach from the ScreenCaptured event handler
_screenSecurity . ScreenCaptured -= OnScreenCaptured ;
base . OnDisappearing ( ) ;
}
private async void OnScreenCaptured ( object sender , EventArgs e )
{
string title = "ScreenSecuritySample" ;
string message = "Screen was captured by screenshot or recording." ;
await Shell . Current . DisplayAlert ( title , message , "Ok" ) ;
}
} 有关此插件的全面实现,请参阅“屏幕”示例,为您提供对其使用情况的完整了解。
如果您遇到任何错误或提交公关以贡献改进或修复程序,请随时打开问题。非常感谢您的贡献。
插件.maui.screensecurity已获得MIT许可证的许可。