截屏状态监听 - iOS
既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Plan B),废话不多说步骤如下.
1 #pragma mark - 监听截屏 2 // Plan A 3 /** 4 监听设备截屏 5 */ 6 - (void)registerTakeScreenShotNotice { 7 kWeakSelf(self); 8 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 9 [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification 10 object:nil 11 queue:mainQueue 12 usingBlock:^(NSNotification * _Nonnull note) { 13 14 NSLog(@"考试截屏"); 15 [weakself userDidTakeScreenshot];//屏幕响应 16 }]; 17 } 18 /** 19 截屏响应 20 */ 21 - (void)userDidTakeScreenshot { 22 NSLog(@"检测到截屏"); 23 //人为操作,获取截屏图片数据 24 UIImage *image = [self imageWithScreenshot]; 25 NSLog(@"userDidTakeScreenshot:\n%@", image); 26 27 UIImageView *imageScreenshot = [[UIImageView alloc] initWithImage:image];// 此处 image 资源可根据实际需求进行操作,展示当前截屏图片或者替换成一张固定的图片方式等等等! 28 imageScreenshot.frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); 29 [self.wkWebView addSubview:imageScreenshot];// 展示在当前 View 层级 30 } 31 /** 32 返回截屏数据 33 34 @return 返回截屏数据 35 */ 36 - (UIImage *)imageWithScreenshot { 37 NSData *imageData = [self dataWithScreenshotInPNGFormat]; 38 return [UIImage imageWithData:imageData]; 39 } 40 /** 41 获取当前屏幕 42 43 @return 获取当前屏幕 44 */ 45 - (NSData *)dataWithScreenshotInPNGFormat { 46 // Source (Under MIT License): 47 CGSize imageSize = CGSizeZero; 48 UIInterfaceOrientation orientation = kApplication.statusBarOrientation; 49 if (UIInterfaceOrientationIsPortrait(orientation)) { 50 imageSize = SCREEN_RECT.size; 51 } 52 else { 53 imageSize = CGSizeMake(SCREEN_HEIGHT, SCREEN_WIDTH); 54 } 55 56 UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 57 CGContextRef context = UIGraphicsGetCurrentContext(); 58 for (UIWindow *window in [kApplication windows]) { 59 CGContextSaveGState(context); 60 CGContextTranslateCTM(context, window.center.x, window.center.y); 61 CGContextConcatCTM(context, window.transform); 62 CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); 63 64 // Correct for the screen orientation 65 if (orientation == UIInterfaceOrientationLandscapeLeft) { 66 CGContextRotateCTM(context, M_PI_2); 67 CGContextTranslateCTM(context, 0, -imageSize.width); 68 } 69 else if (orientation == UIInterfaceOrientationLandscapeRight) { 70 CGContextRotateCTM(context, -M_PI_2); 71 CGContextTranslateCTM(context, -imageSize.height, 0); 72 } 73 else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 74 CGContextRotateCTM(context, M_PI); 75 CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); 76 } 77 78 if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { 79 [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES]; 80 } 81 else { 82 [window.layer renderInContext:context]; 83 } 84 85 CGContextRestoreGState(context); 86 } 87 88 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 89 UIGraphicsEndImageContext(); 90 91 return UIImagePNGRepresentation(image); 92 } 93 94 // Plan B 95 - (void)intercepScreenshots { 96 // kWeakSelf(self); 97 // NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 98 // [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) { 99 // [weakself checkScreenshots]; 100 // }]; 101 [kNotificationCenter addObserver:self 102 selector:@selector(checkScreenshots) 103 name:UIApplicationUserDidTakeScreenshotNotification 104 object:nil]; 105 } 106 - (void)checkScreenshots { 107 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" 108 message:@"勿截图" 109 delegate:self 110 cancelButtonTitle:@"YES" 111 otherButtonTitles:@"NO", nil]; 112 [alertView show]; 113 }
此次分享到此结束,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!
截屏状态监听 - iOS相关推荐
- 录屏状态监听之防录屏 - iOS
继之前接到电话.短信和截屏监听需求之后,在 iOS 11.0 系统之上新增了屏幕录制的新功能玩法,所以也随之迎来了新的屏幕录制监听的需求,即防录屏功能监听 ... 通过官方文档得知 capturedD ...
- Android截屏事件监听
1. 前言 Android系统没有直接对截屏事件监听的接口,也没有广播,只能自己动手来丰衣足食,一般有三种方法. 利用FileObserver监听某个目录中资源变化情况 利用ContentObserv ...
- iOS 防止录屏和截屏的监听
iOS实现不了不让截屏或者录屏,但是提供的截屏或者录屏的监听方法,我们也可以通过监听方法来拿到截屏的图片,为此参考了支付宝和微信支付时,截屏的处理方式. 通过上图发现,其实图片都已经保存到本地相册中, ...
- esc键退出全屏 vue_解决了VUE在浏览器全屏下监听不到Esc键盘事件
说明: 实测可以在谷歌.火狐.360 浏览器使用 解决了在浏览器全屏下监听不到键盘Esc事件 解决了取消全屏和全屏的同步问题,ESC按键下可以同步 以下是完整的代码, // data() { retu ...
- 解决VUE在浏览器全屏下监听不到Esc键盘事件
实测可以在谷歌.火狐.360 浏览器使用 解决了在浏览器全屏下监听不到键盘Esc事件 解决了取消全屏和全屏的同步问题,ESC按键下可以同步 以下是完整的代码, // data() {return {i ...
- android获取wifi开关,Android WiFi开发(一)--WiFi开关与状态监听
之前开发了一个WiFi,热点相关的应用.因为对这方面也不熟悉,刚开始找资料看书,但看明白实现时,发现随着android版本更新,相关api有较大改动,之前的代码不能用.经过一番探索,最后实现出来了,现 ...
- 安卓APP在运行时对全局进行网络状态监听的实现
转载自:https://blog.51cto.com/wangjinchan/4752142 感谢博主:一粒程序米 分享 一.前言 怎么对APP进行全局的网络监听呢?仿照微信的无网状态下弹出的提示,应 ...
- android 蓝牙电话号码,Android拨打电话和蓝牙状态监听
一.拨打电话 权限管理 思路 通过Intent设置Action为ACTION_DIAL或者 ACTION_CALL,通过intent启动直接拨打电话或者打开拨打电话界面. 示例代码 直接拨打电话 /* ...
- android 通话状态监听(自定义接听挂断按钮与通话界面,根据公司的业务逻辑可以实现自己的来电秀功能)...
前言: 因为公司需求,要自定义一款来电秀的app当做周边产品来配合主营的app业务. 之前因为赶项目,没时间整理这块,现在项目告一段落了,现在回头看看感觉这个功能还是挺有意思的,比较有针对性.电话呼入 ...
最新文章
- 急需降低系统复杂性,我们从 Kafka 迁移到了 Pulsar
- 彻底关闭windows server 2008 IPv6
- android开发常见的设计模式,Android开发有哪些常用设计模式?
- Argparse简易教程
- The fall of RNN / LSTM
- 8.16模拟:树上算法
- java 返回页面_spring-mvc返回视图jsp页面及重定向
- AcWing 4241. 货物运输
- yii 下 session 丢失的问题
- 第八讲:1602液晶(郭天祥)
- 《动手学深度学习(PYTORCH版)》第3章引入“d2lzh_pytorch”包报错:No module named ‘torchtext’
- OBS Studio录屏黑屏解决办法win10
- 即将实习的应届毕业生 学习java SpringMVC 数据库 知识总结
- C语言 | 将密码译回原文,并输出密码和原文
- Masonry的简单使用
- 黑客遇到大数据,黑色产业链成暴力行业!
- android 区分 真机模拟器_Android模拟器和真机总结的九点区别 Android程序如何在手机上运行...
- 姫野珠世 - can know two close
- 【云原生之Docker实战】使用Docker部署NPS内网穿透工具
- 仲舟の奇妙算法(二)【个人版】