iOS 防止录屏和截屏的监听

iOS实现不了不让截屏或者录屏,但是提供的截屏或者录屏的监听方法,我们也可以通过监听方法来拿到截屏的图片,为此参考了支付宝和微信支付时,截屏的处理方式。

 通过上图发现,其实图片都已经保存到本地相册中,程序监听到了截图的事件,然后给予友好的提示,接下来说下实现的方法。

截屏状态获取

编辑相册中最新照片的方法iOS8之后就已经失效,框架“Photos”也在iOS10之后失效。

搜索发现UIApplication中仅有用户截屏后的通知,应用中只会收到已经截屏的通知并没办法干预。

?

1

2

// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)

UIKIT_EXTERN NSNotificationNameconst UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);

虽然无法直接干预,但可以知道用户截屏了就可以用其它的方式来限制用户的行为或者弹出提示告诉用户。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

-(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中新增了录屏功能,之前的系统要想录屏,只能通过Airplay 或者屏幕镜像软件,例如 Reflector。有了录屏功能确实方便了用户,但对于一些做内容的公司和网站,特别是视频网站,并不希望自己的付费视频被录制并在互联网上传播。

iOS 11 SDK 中新增了UIScreen的API用以告知应用当前屏幕正在录屏。当UIScreen.isCaptured 为true时,表示当前屏幕正在被录制、镜像或被Airplay 发送。

当录屏状态发生变化时,UIKit会发送UIScreenCapturedDidChange的notification,该notification的object参数即为isCaptured属性发生变化的 UIScreen对象,另外,notification没有userInfo参数。

我们可以在应用中接收此通知,来对用户的录屏行为做相应的处理。比如,在视频app中,我们可以添加通知来监测 UIScreen.isCaptured 的变化,当UIScreen.isCaptured为true时,暂停视屏播放,并弹出提示告知用户,由于正在录屏,不予播放视屏。

基于此,我们可以在应用中接收此通知,来对用户的录屏行为做相应的处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

-(void)viewWillAppear:(BOOL)animated{

  [super viewWillAppear:animated];

 

// 监测当前设备是否处于录屏状态

  UIScreen * sc = [UIScreen mainScreen];

  if (@available(iOS 11.0, *)) {

    if (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之后,而且只是单单的检测到录屏状态并且没有办法去关闭录屏状态或者修改录制到的内容。

找了一些资料,没有找到实现如果截屏后让截屏存在本地相册的图片变为黑色和录屏支持所有系统版本的方法,哪位大牛有相关的代码实现或者思路,请告知参考下。


版权声明:本文为wangxinxu521原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。