Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbinna/1021521 to your computer and use it in GitHub Desktop.
Save mbinna/1021521 to your computer and use it in GitHub Desktop.
Prevent sensitive information from appearing in the screenshot that is automatically taken when the application transitions from the foreground to the background and vice-versa.
//
// UIViewController+MBSensitiveInformationInScreenshotPrevention.h
//
// Created by Manuel Binna on 05.05.11.
// Copyright 2011 Manuel Binna. All rights reserved.
//
@interface UIViewController (MBSensitiveInformationInScreenshotPrevention)
// Hide the root view of the view controller's view hierarchy when the application did enter background or when the application will terminate.
- (void)mb_startPreventingSensitiveInformationFromAppearingInScreenshot;
// Un-hide the root view of the view controller's view hierarchy when the application will enter foreground.
- (void)mb_stopPreventingSensitiveInformationFromAppearingInScreenshot;
@end
//
// UIViewController+MBSensitiveInformationInScreenshotPrevention.m
//
// Created by Manuel Binna on 05.05.11.
// Copyright 2011 Manuel Binna. All rights reserved.
//
#import "UIViewController+MBSensitiveInformationInScreenshotPrevention.h"
static NSMutableArray *mb_registeredObservers;
@implementation UIViewController (MBSensitiveInformationInScreenshotPrevention)
- (void)mb_startPreventingSensitiveInformationFromAppearingInScreenshot {
// Need to store the registered observers to be able to unregister them later.
if (mb_registeredObservers == nil) {
mb_registeredObservers = [[NSMutableArray alloc] initWithCapacity:3];
}
__block __typeof__(self) blockSelf = self; // Prevent creating a retain cycle.
id observer = nil;
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
blockSelf.view.hidden = YES;
}];
[mb_registeredObservers addObject:observer];
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillTerminateNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
blockSelf.view.hidden = YES;
}];
[mb_registeredObservers addObject:observer];
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
blockSelf.view.hidden = NO;
}];
[mb_registeredObservers addObject:observer];
}
- (void)mb_stopPreventingSensitiveInformationFromAppearingInScreenshot {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[mb_registeredObservers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[defaultCenter removeObserver:obj];
}];
[mb_registeredObservers release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment