美文网首页iOS 问题类
iOS 录屏和截屏监听

iOS 录屏和截屏监听

作者: 爱笑的猫mi | 来源:发表于2019-08-08 10:22 被阅读0次

    源起

    最近公司应用安全检查,在用户登录页面要防止截屏和录屏导致用户名密码等敏感信息泄露。
    iOS实现不了不让截屏或者录屏,但是提供的截屏或者录屏的监听方法,当用户截屏或录屏时系统会发送相关通知,我们可以提示用户截屏或录屏会泄露一些个人安全信息,类似于微信或支付宝的付款码截屏。
    截屏通知名:UIApplicationUserDidTakeScreenshot
    录屏是iOS11之后才有的功能,UIScreen 的isCaptured方法可以捕获此屏幕状态变化,例如,录制、空中播放、镜像就会为真。 录屏通知名:UIScreenCapturedDidChange,我们也可以监听录屏通知。

    效果

    /1.屏幕截屏


    /2.屏幕录制


    WechatIMG1508.png

    具体步骤和使用原理

    1.截屏状态获取

    编辑相册中最新照片的方法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)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor =[UIColor whiteColor];
        [self setSubView];
        
        //增加监听->监听截图事件
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleSceenShot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    }
    
    //当用户截屏了 怎么办 目前来说 只能进行提示。
    -(void)handleSceenShot {
        UIAlertController * alertVc =[UIAlertController alertControllerWithTitle:@"信息提示" message:@"为保证用户名,密码安全,请不要截屏或录屏!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * knowAction =[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
        [alertVc addAction:knowAction];
        [self presentViewController:alertVc animated:YES completion:nil];
    }
    
    -(void)dealloc {
       
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    }
    
    

    2.录屏状态获取

    录制屏幕有2种情况。一种是在应用该界面 先退出打开录制工具按钮,再回到程序。二是优先打开了录制按钮,直接进入到录制界面。

    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 tipsVideoRecord];
            }
        }else {
           //ios 11之前处理 未知
        }
        
        //ios11之后才可以录屏
        if (@available(iOS 11.0,*)) {
          //检测设备
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tipsVideoRecord) name:UIScreenCapturedDidChangeNotification  object:nil];
        }
    }
    
    -(void)dealloc {
        
        if (@available(iOS 11.0, *)) {
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenCapturedDidChangeNotification object:nil];
        } else {
            // Fallback on earlier versions
        }
    }
    //当用户录屏 怎么办 目前来说 只能进行提示。
    -(void)tipsVideoRecord {
        UIAlertController * alertVc =[UIAlertController alertControllerWithTitle:@"信息提示" message:@"为保证用户名,密码安全,请不要截屏或录屏!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * knowAction =[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
        [alertVc addAction:knowAction];
        [self presentViewController:alertVc animated:YES completion:nil];
        
    }
    
    

    总结

    监听录制或者截屏功能在一些App中还是很重要的功能,比如视频会话,App 之间的,如果一方通过屏幕录制进行了非合理的操作,可以通过长链接的通话,监听到该操作上传服务器,然后通知给视频对方。这样会让我们的聊天或者视频会议等更加透明和不存在侵权等行为。当然金融类App也是会较多采用。

    相关文章

      网友评论

        本文标题:iOS 录屏和截屏监听

        本文链接:https://www.haomeiwen.com/subject/nbuodctx.html