既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 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的更多相关文章

  1. 录屏状态监听之防录屏 - iOS

    继之前接到电话.短信和截屏监听需求之后,在 iOS 11.0 系统之上新增了屏幕录制的新功能玩法,所以也随之迎来了新的屏幕录制监听的需求,即防录屏功能监听 ... 通过官方文档得知 capturedD ...

  2. ios截屏事件监听

    目的:实现截屏反馈,类似支付宝的截屏上传反馈功能. 1.注册全局通知,在Appdelegate中注册截屏监听通知 - (void)registNotification{ [[NSNotificatio ...

  3. 电话状态监听 - iOS

    今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...

  4. 短信状态监听 - iOS

    当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...

  5. TESTNG重试、截屏、监听

    http://qa.blog.163.com/blog/static/19014700220138585422735/

  6. Android USB大容量存储时SD卡状态监听(转)

    对SD卡状态监听,到现在为止我知道的有两种方式: 1.注册StorageEventListener来监听sd卡状态 StorageEventListener中有onStorageStateChange ...

  7. JS控制全屏,监听退出全屏事件

    实现方案 //进入全屏 function requestFullScreen(de) { if(de.requestFullscreen){ //W3C de.requestFullscreen(); ...

  8. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  9. iOS实现电话状态监听 CoreTelephony

    在程序中如果需要监听电话状态,可以引入CoreTelephony框架,这个框架包含了电话相关的API,可以实现监测来电,查看运营商信息等功能.下面就是具体的实现监测来电的代码.一定要把center写成 ...

随机推荐

  1. Android内存管理-OnTrimMemory

    Application中有两个与内存管理相关的方法:onLowMemory()和 onTrimMemory(int level),源码如下 @CallSuper public void onLowMe ...

  2. centos虚拟机下安装nginx

    通过yum安装 yum install epel-release -y(企业级的镜像源) yum install nginx-y 启动.停止.重启 service nginx start servic ...

  3. JAVA后台框架优化之日志篇

    1.日志规范 各业务系统日志需要统一,以方便查看.收集日志, 日后统一ELK日志管理,以下为项目的日志配置, 这是兼容当前系统的日志,以后推行微服架构时会有变动,但日志存放方式不会改变,日后会推行sp ...

  4. 封装网络请求并在wxml调用

    https://blog.csdn.net/qq_35713752/article/details/78109084 // url:网络请求的url method:网络请求方式 data:请求参数 m ...

  5. OS考研复习笔记——操作系统的定义、目标、作用和发展的主要动力

    计算机系统由硬件和软件两部分组成.操作系统(OS,Operating System)是配置在计算机硬件上的第一层软件,是对硬件系统的首次补充. 硬件:计算机物理设备,即各种处理机存储器.输入/输出设备 ...

  6. C# 平台问题

    最近在C#项目中嵌入一个视频软件Ffplayer,出现报错现象,提示平台开发视频.dll文件的兼容性和加载格式不正确的问题.最终查看是由于项目平台选择的是Any CPU和X86的引起的.目标平台有什么 ...

  7. Mvnw 简介

    Mvnw 简介  8月 17, 2016 |  Nix.Huang 背景 maven是一款非常流行的java项目构建软件,它集项目的依赖管理.测试用例运行.打包.构件管理于一身,是我们工作的好帮手,m ...

  8. Python学习---Django的新工程设置模板

    该模板完全可以在创建好新工程后进行部分代码替换 创建app01的  python startapp app01   创建static子目录 settings.py """ ...

  9. 沉淀再出发:如何在eclipse中查看java的核心代码

    沉淀再出发:如何在eclipse中查看java的核心代码 一.前言   很多时候我们在eclipse中按F3键打算查看某一个系统类的定义的时候,总是弹出找不到类这样的界面,这里我们把核心对应的代码加进 ...

  10. codeforces 453C Little Pony and Summer Sun Celebration

    codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...