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許可證的許可。