既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 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. 转:Redis和Memcache的区别分析

    Redis和Memcache的区别分析  原文链接:http://blog.csdn.net/u013474436/article/details/48632665 简单区别: 1. Redis中,并 ...

  2. Java设计模式—享元模式

    享元模式:是池技术的重要实现方式. 定义如下: 使用共享对象可有效地支持大量的细粒度的对象. 个人理解:享元模式利用共享对象的技术,解决了Java中内存溢出的问题. 享元模式的定义为我们提出了两个要求 ...

  3. 你真的了解Fragment的生命周期吗?

    Android Framwork开发人员中的传奇人物Dianne Hackborn在2010年将Fragment引入了Android,也就是在android3.0之后引入Fragment,他在提交信息 ...

  4. Linux 虚拟机配置-network is unreachable

    配置虚拟机时,遇到network is unreachable,根据网上找来的方法处理,最终自己试过,成功修改的方法在这里记录一下: 修改虚拟机的网络适配器:桥接,复制物理机网络 vim /etc/s ...

  5. C语言二级指针(指向指针的指针)

    转载:http://c.biancheng.net/cpp/html/85.html 指针可以指向一份普通类型的数据,例如 int.double.char 等,也可以指向一份指针类型的数据,例如 in ...

  6. Java基础之StringBuffer和StringBuilder的区别

    StringBuffer是一个字符串的缓存类,属于一个容器,对于容器,我们可以进行增删改查. StringBuffer的容器长度是可变的,并且里面可以存放多种的数据类型.它跟其他容器,比如数组,是很不 ...

  7. iOS设计模式 - 桥接

    iOS设计模式 - 桥接 示意图 说明 1. 桥接模式为把抽象层次结构从实现中分离出来,使其可以独立变更,抽象层定义了供客户端使用的上层抽象接口,实现层次结构定义了供抽象层次使用的底层接口,实现类的引 ...

  8. 《C++ Primer Plus》读书笔记之四—分支语句和逻辑操作符

    第六章 分支语句和逻辑操作符 1.&&的优先级低于关系操作符. 2.取值范围:取值范围的每一部分都使用AND操作符将两个完整的关系表达式组合起来: if(age>17&& ...

  9. (转)如何解决VC中的警告warning C4251 needs to have dll-interface

    这通常是由于以数据成员方式在DLL导出类中使用了模板类造成的.比如: #include <iostream> #include <vector> using namespace ...

  10. 如何恢复在Windows 10中被永久删除的照片?

    照片被误删除了需要恢复?这里推荐一款软件:winutilities.使用WinUtilities文件恢复向导允许您通过简单的点击恢复已删除的照片或从Windows 10回收站中恢复被删除的照片. 恢复 ...