截屏状态监听 - iOS
既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Plan B),废话不多说步骤如下.
#pragma mark - 监听截屏
// Plan A
/**
监听设备截屏
*/
- (void)registerTakeScreenShotNotice {
kWeakSelf(self);
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification * _Nonnull note) { NSLog(@"考试截屏");
[weakself userDidTakeScreenshot];//屏幕响应
}];
}
/**
截屏响应
*/
- (void)userDidTakeScreenshot {
NSLog(@"检测到截屏");
//人为操作,获取截屏图片数据
UIImage *image = [self imageWithScreenshot];
NSLog(@"userDidTakeScreenshot:\n%@", image); UIImageView *imageScreenshot = [[UIImageView alloc] initWithImage:image];// 此处 image 资源可根据实际需求进行操作,展示当前截屏图片或者替换成一张固定的图片方式等等等!
imageScreenshot.frame = CGRectMake(SCREEN_WIDTH / , SCREEN_HEIGHT / , SCREEN_WIDTH / , SCREEN_HEIGHT / );
[self.wkWebView addSubview:imageScreenshot];// 展示在当前 View 层级
}
/**
返回截屏数据 @return 返回截屏数据
*/
- (UIImage *)imageWithScreenshot {
NSData *imageData = [self dataWithScreenshotInPNGFormat];
return [UIImage imageWithData:imageData];
}
/**
获取当前屏幕 @return 获取当前屏幕
*/
- (NSData *)dataWithScreenshotInPNGFormat {
// Source (Under MIT License):
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = kApplication.statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
imageSize = SCREEN_RECT.size;
}
else {
imageSize = CGSizeMake(SCREEN_HEIGHT, SCREEN_WIDTH);
} UIGraphicsBeginImageContextWithOptions(imageSize, NO, );
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [kApplication windows]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); // Correct for the screen orientation
if (orientation == UIInterfaceOrientationLandscapeLeft) {
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, , -imageSize.width);
}
else if (orientation == UIInterfaceOrientationLandscapeRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, );
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
} if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
}
else {
[window.layer renderInContext:context];
} CGContextRestoreGState(context);
} UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return UIImagePNGRepresentation(image);
} // Plan B
- (void)intercepScreenshots {
// kWeakSelf(self);
// NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
// [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {
// [weakself checkScreenshots];
// }];
[kNotificationCenter addObserver:self
selector:@selector(checkScreenshots)
name:UIApplicationUserDidTakeScreenshotNotification
object:nil];
}
- (void)checkScreenshots {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"勿截图"
delegate:self
cancelButtonTitle:@"YES"
otherButtonTitles:@"NO", nil];
[alertView show];
}
此次分享到此结束,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!
截屏状态监听 - iOS的更多相关文章
- 录屏状态监听之防录屏 - iOS
继之前接到电话.短信和截屏监听需求之后,在 iOS 11.0 系统之上新增了屏幕录制的新功能玩法,所以也随之迎来了新的屏幕录制监听的需求,即防录屏功能监听 ... 通过官方文档得知 capturedD ...
- ios截屏事件监听
目的:实现截屏反馈,类似支付宝的截屏上传反馈功能. 1.注册全局通知,在Appdelegate中注册截屏监听通知 - (void)registNotification{ [[NSNotificatio ...
- 电话状态监听 - iOS
今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...
- 短信状态监听 - iOS
当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...
- TESTNG重试、截屏、监听
http://qa.blog.163.com/blog/static/19014700220138585422735/
- Android USB大容量存储时SD卡状态监听(转)
对SD卡状态监听,到现在为止我知道的有两种方式: 1.注册StorageEventListener来监听sd卡状态 StorageEventListener中有onStorageStateChange ...
- JS控制全屏,监听退出全屏事件
实现方案 //进入全屏 function requestFullScreen(de) { if(de.requestFullscreen){ //W3C de.requestFullscreen(); ...
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- iOS实现电话状态监听 CoreTelephony
在程序中如果需要监听电话状态,可以引入CoreTelephony框架,这个框架包含了电话相关的API,可以实现监测来电,查看运营商信息等功能.下面就是具体的实现监测来电的代码.一定要把center写成 ...
随机推荐
- 命令行下运行 java someClass.class出现 “错误:找不到或无法加载主类someClass ” 的解决方案
假设在C:\Java\code\目录下建立了如下 Test.java文件: package code; public class Test { public static void main(Stri ...
- Arcgis GDB文件地理数据库、shapefile、coverage 和其他基于文件的数据源所支持的函数的完整列表
函数 以下是文件地理数据库.shapefile.coverage 和其他基于文件的数据源所支持的函数的完整列表.个人地理数据库和 ArcSDE 地理数据库也支持这些函数,但这些数据源可能使用不同的语法 ...
- Android 获取SD卡的图片资源
首先我先获得SD卡下的根目录路径: privateString isSdcard(){ File sdcardDir=null; boolean isSDExist=Environment.getEx ...
- Python问题1:IndentationError:expected an indented block
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...
- Windows API 查找窗体,发送Windows消息
最近项目中需要做Windows消息截获操作,在网上找了一些资料. public class WindowsAPI { /// <summary> /// 回调函数代理 /// </s ...
- 译文 [ROM][多国语言][2015.06.11] Lenovo S750 (MTK6589) - andrea_d86-lenovos750-4.2.2
************************************************** andrea_d86-lenovos750-4.2.2-150530 ************** ...
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 省钱小贴士(ECS):教你如何每年省出8w+ 块
随着用户越来越多地使用阿里云的ECS服务,如何用最小的成本来保有ECS,成为用户越来越重要的关注点. 变更点 为了更好的服务客户,ECS团队调整了系统盘的最小容量限制 linux系统 core os调 ...
- 使用Visual Studio Code开发Arduino
首发于MSPrecious成长荟 https://zhuanlan.zhihu.com/p/30868224 使用Visual Studio Code开发Arduino 1.下载安装 VS Code ...
- ZT 蓝牙的AVCTP协议笔记
蓝牙的AVCTP协议笔记 (2013-07-31 08:52:41) 转载▼ 标签: bluetooth avctp command response 分类: Bluetooth 1.概述 A ...