截屏状态监听 - 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写成 ...
随机推荐
- WinForm实现Rabbitmq官网6个案例-RPC
获取源码 客户端代码: namespace RabbitMQDemo { public partial class RPC : Form { private readonly static RPC _ ...
- Unity射线
//射线原点 [SerializField] Transform tr; //射线长度 [SerializField] float dis = 5; //射线停留时间 [SerializFiel ...
- 网页简单布局之结构与表现原则(HTML/CSS)
结构 样式 行为真正的分离 前端初级人员会在页面上单纯的用各个div把相关内容独立开: 前端中级人员明白相关属性的设置会给元素带来什么改变,从而减少div的书写: 前端高级人员会以及其简单的和稳定的方 ...
- 【转】fatal error C1189: #error : missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
转自:http://blog.csdn.net/friendan/article/details/46576699 fatal error C1189: #error : missing -D__S ...
- 在Windows下为PHP5.6安装redis扩展和memcached扩展
一.php安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...
- 毕向东_Java基础视频教程第20天_IO流(15~17)
第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...
- ASP.NET错误处理的方式(二)
要创建页中的全局处理程序,请创建 Page_Error 事件的处理程序.要创建应用程序范围的错误处理程序,请在 Global.asax 文件中将代码添加到 Application_Error 方法.只 ...
- find bugs设置
- Source InSight context 窗口丢失的解决办法
我没关si的情况下强制关机导致的 Source InSight context 窗口丢失的解决办法 (2010-09-03 13:35:45) 转载▼ 今天早晨改改CLI,上了趟WC,回来发现 ...
- [Codeup 25481] swan
莫名其妙还找到了另一个铟炔锶烃的OJ : Codeup墓地 25481: swan 时间限制: 1 Sec 内存限制: 128 MB献花: 86 解决: 13[献花][花圈][TK题库] 题目描述 ...