最近在做項目安全性方面的工作,需要在APP內敏感頁面做防用戶截屏錄屏的功能,就在網上查閱了一些資料,在這里做個筆記,方便日后查找。
截屏狀態獲取
編輯相冊中最新照片的方法iOS8之后就已經失效,框架“Photos”也在iOS10之后失效。
搜索發現UIApplication中僅有用戶截屏后的通知,應用中只會收到已經截屏的通知并沒辦法干預。
// 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);
雖然無法直接干預,但可以知道用戶截屏了就可以用其它的方式來限制用戶的行為或者彈出提示告訴用戶。
-(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:@"[安全提醒]內含個人資金賬戶。不要截圖,錄制或分享給他人以保障資金賬戶安全。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil]; [alert1 show];-(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];}
錄屏狀態獲取
iOS 11 SDK 中新增了UIScreen的API用以告知應用當前屏幕正在錄屏。當UIScreen.isCaptured 為true時,表示當前屏幕正在被錄制、鏡像或被Airplay 發送。
當錄屏狀態發生變化時,UIKit會發送UIScreenCapturedDidChange的notification。
基于此,我們可以在應用中接收此通知,來對用戶的錄屏行為做相應的處理
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];//監測當前設備是否處于錄屏狀態 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, *)) {//檢測到當前設備錄屏狀態發生變化 [[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:@"[安全提醒]內含個人資金賬戶。不要截圖,錄制或分享給他人以保障資金賬戶安全。" 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 }}
上述監測錄屏狀態只是在iOS11之后,而且只是單單的檢測到錄屏狀態并且沒有辦法去關閉錄屏狀態或者修改錄制到的內容,至于在iOS11之前的錄屏手段的監測暫時還沒查到,有哪位大神知道的話麻煩告知小弟,在此謝過。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答