梦乡.jpeg
ios不能做到禁止截屏和录屏,只能使用通知监听到截屏或录屏事件,来做一些处理,比如停止播放视频等。
如果想要监听整个app运行期间的所有页面,则把监听方法写到appdelegate中;如果只是监听指定页面,则把监听方法写到指定页面。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 截屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeScreenTest) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
// iOS11后中新增了录屏功能
if (@available(iOS 11.0, *)) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureScreenTest) name:UIScreenCapturedDidChangeNotification object:nil];
}
return YES;
}
-(void)takeScreenTest{
[self alertTakeScreenWithStr:@"您在截屏,请注意保护个人隐私"];
}
-(void)captureScreenTest{
if (@available(iOS 11.0, *)) {
// 开始录屏时有弹框提示,结束录屏时就不弹框了。
if (![UIScreen mainScreen].isCaptured) {
return;
}
[self alertTakeScreenWithStr:@"您在录屏,请注意保护个人隐私"];
}
}
-(void)alertTakeScreenWithStr:(NSString *)str{
NSLog(@"监听到截屏或录屏");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定");
}];
[alertController addAction:okAction];
// 如果是页面,用self;如果在appdelegate中,用self.window.rootViewController
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
做了个demo,传到了github仓库,需要的可以下载看看:https://github.com/zhuzi55/BanTakeScreenDemo.git。