既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 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. jquery-animate()动画

    一.animate()语法 $(“选择器”).animate({CSS样式},时间,运动方式,回调函数); 参数说明: 参数1:CSS属性名,属性值,JSON格式{"属性名":&q ...

  2. maven(18)-mybatis generator插件

     generator的作用 使用mybatis框架,在初始项目或修改数据库时,相应的要在JAVA项目中去写一些数据模型文件,DAO,映射XML等配置,而这个插件的作用就是自动生成这些文件,以节省大 ...

  3. django从1.7升级到1.9后 提示:RemovedInDjango110Warning

    Django项目,把django从1.7升级到1.9后,大量报错.需要做如下修改. 1,修改urls.py: 在django1.9里,urls的配置不再支持字符串型的路由.需要先import,然后直接 ...

  4. python面向对象编程(1)——基本概念,术语,self,构造器

    1  python面向对象命名规范 类名通常由大写字母打头.这是惯例标准. 数据值应该使用名词作为名字,方法使用动词加对象的方式,若使用混合记法,则方法名的第一个字母首字母小写,后面的单词的首字母大写 ...

  5. switch与java

    switch结构可以更好的解决等值判断问题switch 选择结构的语法:switch (表达式){case 常量 1://代码块1:break;case 常量 2://代码块2:break;..... ...

  6. cygwin64-安装包管理工具

    1.dos command, install pkg $ setup-x86_64.exe -q -P curl $ setup-x86_64.exe -q -P lynx 2. cygwin64 c ...

  7. maven将依赖的包一起打包

    把以下内容输入到pom中即可 <build> <plugins> <!-- 将项目的依赖包复制到 target/lib --> <plugin> < ...

  8. 2 Docker 镜像基础

    Docker 镜像可以从docker.io 下载,也可以自己通过Dockerfile来构建镜像,我有时从国外下载镜像时,网速不行,我就改成国内的镜像,修改如下: # vim /etc/docker/d ...

  9. redis-day1

    1 Redis 概述 REmote DIctionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统.redis和大名鼎鼎的Memcached缓存服务软件很像, ...

  10. 10个值得深思的PHP面试题

    第一个问题关于弱类型 $str1 = 'yabadabadoo'; $str2 = 'yaba'; if (strpos($str1,$str2)) { echo "/"" ...