当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS录屏和截屏监听的实现代码

iOS录屏和截屏监听的实现代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近在做项目安全性方面的工作,需要在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之前的录屏手段的监测暂时还没查到,有哪位大神知道的话麻烦告知小弟,在此谢过。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网