截屏状态监听 - 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 / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
    [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, 0);
    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, 0, -imageSize.width);
        }
        else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        }
        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];
}

 

此次分享到此结束,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值