After receiving the call status monitoring demand, the screenshot status monitoring is added again. When using App, if the user needs to monitor the current status when performing screenshot operation, there are two methods below. One is to replace the screenshot image content (Plan A), the other is to pop up the prompt box (Plan B). The steps are as follows
1 #pragma mark - Monitor screenshots 2 // Plan A 3 /** 4 Screenshot of monitoring equipment 5 */ 6 - (void)registerTakeScreenShotNotice { 7 kWeakSelf(self); 8 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 9 [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification 10 object:nil 11 queue:mainQueue 12 usingBlock:^(NSNotification * _Nonnull note) { 13 14 NSLog(@"Test screenshots"); 15 [weakself userDidTakeScreenshot];//Screen response 16 }]; 17 } 18 /** 19 Screenshots response 20 */ 21 - (void)userDidTakeScreenshot { 22 NSLog(@"Screenshot detected"); 23 //Artificial operation,Get screenshot data 24 UIImage *image = [self imageWithScreenshot]; 25 NSLog(@"userDidTakeScreenshot:\n%@", image); 26 27 UIImageView *imageScreenshot = [[UIImageView alloc] initWithImage:image];// here image Resources can be operated according to actual needs,Display the current screenshot or replace it with a fixed image, etc! 28 imageScreenshot.frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); 29 [self.wkWebView addSubview:imageScreenshot];// Show in current View Hierarchy 30 } 31 /** 32 Return to screenshot data 33 34 @return Return to screenshot data 35 */ 36 - (UIImage *)imageWithScreenshot { 37 NSData *imageData = [self dataWithScreenshotInPNGFormat]; 38 return [UIImage imageWithData:imageData]; 39 } 40 /** 41 Get current screen 42 43 @return Get current screen 44 */ 45 - (NSData *)dataWithScreenshotInPNGFormat { 46 // Source (Under MIT License): 47 CGSize imageSize = CGSizeZero; 48 UIInterfaceOrientation orientation = kApplication.statusBarOrientation; 49 if (UIInterfaceOrientationIsPortrait(orientation)) { 50 imageSize = SCREEN_RECT.size; 51 } 52 else { 53 imageSize = CGSizeMake(SCREEN_HEIGHT, SCREEN_WIDTH); 54 } 55 56 UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 57 CGContextRef context = UIGraphicsGetCurrentContext(); 58 for (UIWindow *window in [kApplication windows]) { 59 CGContextSaveGState(context); 60 CGContextTranslateCTM(context, window.center.x, window.center.y); 61 CGContextConcatCTM(context, window.transform); 62 CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); 63 64 // Correct for the screen orientation 65 if (orientation == UIInterfaceOrientationLandscapeLeft) { 66 CGContextRotateCTM(context, M_PI_2); 67 CGContextTranslateCTM(context, 0, -imageSize.width); 68 } 69 else if (orientation == UIInterfaceOrientationLandscapeRight) { 70 CGContextRotateCTM(context, -M_PI_2); 71 CGContextTranslateCTM(context, -imageSize.height, 0); 72 } 73 else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 74 CGContextRotateCTM(context, M_PI); 75 CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); 76 } 77 78 if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { 79 [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES]; 80 } 81 else { 82 [window.layer renderInContext:context]; 83 } 84 85 CGContextRestoreGState(context); 86 } 87 88 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 89 UIGraphicsEndImageContext(); 90 91 return UIImagePNGRepresentation(image); 92 } 93 94 // Plan B 95 - (void)intercepScreenshots { 96 // kWeakSelf(self); 97 // NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 98 // [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) { 99 // [weakself checkScreenshots]; 100 // }]; 101 [kNotificationCenter addObserver:self 102 selector:@selector(checkScreenshots) 103 name:UIApplicationUserDidTakeScreenshotNotification 104 object:nil]; 105 } 106 - (void)checkScreenshots { 107 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Tips" 108 message:@"Do not screenshots" 109 delegate:self 110 cancelButtonTitle:@"YES" 111 otherButtonTitles:@"NO", nil]; 112 [alertView show]; 113 }
This is the end of the sharing. I hope the content can help you in practice. If you have any shortcomings, please point out common progress!