Forked from mbinna/UIViewController+MBSensitiveInformationInScreenshotPrevention.h
Created
June 12, 2011 21:32
-
-
Save darkseed/1022008 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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