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

2025-05-29 0 50

最近在做项目安全性方面的工作,需要在APP内敏感页面做防用户截屏录屏的功能,就在网上查阅了一些资料,在这里做个笔记,方便日后查找。

截屏状态获取

编辑相册中最新照片的方法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 NSNotificationName const 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 SDK 中新增了UIScreen的API用以告知应用当前屏幕正在录屏。当UIScreen.isCaptured 为true时,表示当前屏幕正在被录制、镜像或被Airplay 发送。

录屏状态发生变化时,UIKit会发送UIScreenCapturedDidChange的notification。

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

?

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) {

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之前的录屏手段的监测暂时还没查到,有哪位大神知道的话麻烦告知小弟,在此谢过。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS录屏和截屏监听的实现代码 https://www.kuaiidc.com/89911.html

相关文章

发表评论
暂无评论