最近在做項目安全性方面的工作惧蛹,需要在APP內(nèi)敏感頁面做防用戶截屏錄屏的功能撩银,就在網(wǎng)上查閱了一些資料,在這里做個筆記鬼佣,方便日后查找驶拱。
截屏狀態(tài)獲取
編輯相冊中最新照片的方法iOS8之后就已經(jīng)失效,框架“Photos”也在iOS10之后失效晶衷。
搜索發(fā)現(xiàn)UIApplication中僅有用戶截屏后的通知蓝纲,應(yīng)用中只會收到已經(jīng)截屏的通知并沒辦法干預(yù)。
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
UIKIT_EXTERN NSNotificationName const UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);
雖然無法直接干預(yù)晌纫,但可以知道用戶截屏了就可以用其它的方式來限制用戶的行為或者彈出提示告訴用戶税迷。
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
-(void)screenshots
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[安全提醒]內(nèi)含個人資金賬戶。不要截圖锹漱,錄制或分享給他人以保障資金賬戶安全箭养。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert1 show];
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
錄屏狀態(tài)獲取
iOS 11 SDK 中新增了UIScreen的API用以告知應(yīng)用當(dāng)前屏幕正在錄屏。當(dāng)UIScreen.isCaptured 為true時哥牍,表示當(dāng)前屏幕正在被錄制毕泌、鏡像或被Airplay 發(fā)送。
當(dāng)錄屏狀態(tài)發(fā)生變化時嗅辣,UIKit會發(fā)送UIScreenCapturedDidChange的notification撼泛。
基于此,我們可以在應(yīng)用中接收此通知澡谭,來對用戶的錄屏行為做相應(yīng)的處理
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//監(jiān)測當(dāng)前設(shè)備是否處于錄屏狀態(tài)
UIScreen * sc = [UIScreen mainScreen];
if (@available(iOS 11.0, *)) {
if (sc.isCaptured) {
NSLog(@"正在錄制~~~~~~~~~%d",sc.isCaptured);
[self screenshots];
}
} else {
// Fallback on earlier versions
}
if (@available(iOS 11.0, *)) {
//檢測到當(dāng)前設(shè)備錄屏狀態(tài)發(fā)生變化
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIScreenCapturedDidChangeNotification object:nil];
} else {
// Fallback on earlier versions
}
}
-(void) screenshots
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[安全提醒]內(nèi)含個人資金賬戶愿题。不要截圖,錄制或分享給他人以保障資金賬戶安全。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert1 show];
-(void)dealloc
{
if (@available(iOS 11.0, *)) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenCapturedDidChangeNotification object:nil];
} else {
// Fallback on earlier versions
}
}
上述監(jiān)測錄屏狀態(tài)是在iOS11之后抠忘,而且只是單單的檢測到錄屏狀態(tài)并且沒有辦法去關(guān)閉錄屏狀態(tài)或者修改錄制到的內(nèi)容撩炊,至于在iOS11之前則是沒有錄屏功能的。