Skip to content

Commit

Permalink
Callback for enabling passcode
Browse files Browse the repository at this point in the history
Added `enablePasscodeWhenApplicationEntersBackground`. It does the opposite of `disablePasscodeWhenApplicationEntersBackground`.
  • Loading branch information
Roland Leth authored and Roland Leth committed Oct 24, 2016
1 parent 610a30e commit 958b322
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 3.7.10
* New delegate method: `passcodeWasEnabled`. Called when the passcode was enabled (Closed [#156](https://github.com/rolandleth/LTHPasscodeViewController/issues/156)).
* New method: `enablePasscodeWhenApplicationEntersBackground`. It reverts what `disablePasscodeWhenApplicationEntersBackground` does: it adds observers for `UIApplicationDidEnterBackgroundNotification` and `UIApplicationWillEnterForegroundNotification` (Closed [#158](https://github.com/rolandleth/LTHPasscodeViewController/issues/158)).

# 3.7.9
* Keychain leaks fixed.
* Back button fixed, it now properly resets state.

# 3.7.8
* Improved keyboard handling when displaying the lockscreen for the first time with TouchID enabled.
* Fixed the bug where the keyboard was invisible after canceling the TouchID alert.
Expand Down
4 changes: 4 additions & 0 deletions Demo/LTHPasscodeViewController Demo/LTHDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ - (void)logoutButtonWasPressed {
NSLog(@"Logout Button Was Pressed");
}

- (void)passcodeWasEnabled {
NSLog(@"Passcode Was Enabled");
}


@end
4 changes: 2 additions & 2 deletions LTHPasscodeViewController/LTHKeychainUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ + (NSString *)getPasswordForUsername:(NSString *)username andServiceName:(NSStri

CFTypeRef attrResult = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) attributeQuery,
&attrResult);
(CFTypeRef *) &attrResult);
if (attrResult) CFRelease(attrResult);

if (status != noErr) {
Expand All @@ -95,7 +95,7 @@ + (NSString *)getPasswordForUsername:(NSString *)username andServiceName:(NSStri

CFTypeRef resData = NULL;
status = SecItemCopyMatching((__bridge CFDictionaryRef) passwordQuery,
&resData);
(CFTypeRef *) &resData);
NSData *resultData = (__bridge_transfer NSData *)resData;

if (status != noErr) {
Expand Down
12 changes: 10 additions & 2 deletions LTHPasscodeViewController/LTHPasscodeViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
@brief Called when the passcode was entered successfully.
*/
- (void)passcodeWasEnteredSuccessfully;
/**
@brief Called when the passcode was enabled.
*/
- (void)passcodeWasEnabled;
/**
@brief Called when the logout button was pressed.
*/
Expand Down Expand Up @@ -323,9 +327,13 @@
*/
- (void)setIsSimple:(BOOL)isSimple inViewController:(UIViewController *)viewController asModal:(BOOL)isModal;
/**
@brief The passcode view will be shown by default when entering the app from background. This method disables this behavior by removing the observer for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification.
@brief The passcode view will be shown by default when entering the app from background. This method disables this behavior by removing the observers for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification.
*/
- (void)disablePasscodeWhenApplicationEntersBackground;
/**
@brief The passcode view will be shown by default when entering the app from background. `disablePasscodeWhenApplicationEntersBackground` can disable that behavior and this method enables it again, by adding back the observers for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification
*/
- (void)enablePasscodeWhenApplicationEntersBackground;
/**
@brief Returns a Boolean value that indicates whether a passcode exists (@c YES) or not (@c NO).
@return @c YES if a passcode is enabled. This also means it is enabled, unless custom logic was added to the library.
Expand Down Expand Up @@ -378,4 +386,4 @@
*/
+ (instancetype)sharedUser;

@end
@end
54 changes: 36 additions & 18 deletions LTHPasscodeViewController/LTHPasscodeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ @interface LTHPasscodeViewController () <UITextFieldDelegate>
@property (nonatomic, assign) CGFloat iPadFontSizeModifier;
@property (nonatomic, assign) CGFloat iPhoneHorizontalGap;

@property (nonatomic, assign) BOOL passcodeAlreadyExists;
@property (nonatomic, assign) BOOL usesKeychain;
@property (nonatomic, assign) BOOL displayedAsModal;
@property (nonatomic, assign) BOOL displayedAsLockScreen;
Expand Down Expand Up @@ -258,6 +259,13 @@ - (void)_deletePasscode {


- (void)_savePasscode:(NSString *)passcode {
if (!_passcodeAlreadyExists &&
[self.delegate respondsToSelector:@selector(passcodeWasEnabled)]) {
[self.delegate passcodeWasEnabled];
}

_passcodeAlreadyExists = YES;

if (!_usesKeychain &&
[self.delegate respondsToSelector:@selector(savePasscode:)]) {
[self.delegate savePasscode:passcode];
Expand Down Expand Up @@ -487,10 +495,7 @@ - (void)_cancelAndDismissMe {
if ([self.delegate respondsToSelector: @selector(passcodeViewControllerWillClose)]) {
[self.delegate performSelector: @selector(passcodeViewControllerWillClose)];
}
// Or, if you prefer by notifications:
// [[NSNotificationCenter defaultCenter] postNotificationName: @"passcodeViewControllerWillClose"
// object: self
// userInfo: nil];

if (_displayedAsModal) [self dismissViewControllerAnimated:YES completion:nil];
else if (!_displayedAsLockScreen) [self.navigationController popViewControllerAnimated:YES];
}
Expand Down Expand Up @@ -542,10 +547,7 @@ - (void)_dismissMe {
if ([self.delegate respondsToSelector: @selector(passcodeViewControllerWillClose)]) {
[self.delegate performSelector: @selector(passcodeViewControllerWillClose)];
}
// Or, if you prefer by notifications:
// [[NSNotificationCenter defaultCenter] postNotificationName: @"passcodeViewControllerWillClose"
// object: self
// userInfo: nil];

if (_displayedAsLockScreen) {
[self.view removeFromSuperview];
[self removeFromParentViewController];
Expand Down Expand Up @@ -1048,6 +1050,7 @@ - (void)_prepareNavigationControllerWithController:(UIViewController *)viewContr
- (void)showForEnablingPasscodeInViewController:(UIViewController *)viewController
asModal:(BOOL)isModal {
_displayedAsModal = isModal;
_passcodeAlreadyExists = NO;
[self _prepareForEnablingPasscode];
[self _prepareNavigationControllerWithController:viewController];
self.title = LTHPasscodeViewControllerStrings(self.enablePasscodeString);
Expand Down Expand Up @@ -1222,10 +1225,6 @@ - (BOOL)_validatePasscode:(NSString *)typedString {
// App launch/Turning passcode off: Passcode OK -> dismiss, Passcode incorrect -> deny access.
else {
if ([typedString isEqualToString: savedPasscode]) {
// Or, if you prefer by notifications:
// [[NSNotificationCenter defaultCenter] postNotificationName: @"passcodeWasEnteredSuccessfully"
// object: self
// userInfo: nil];
[self _dismissMe];
_useFallbackPasscode = NO;
if ([self.delegate respondsToSelector: @selector(passcodeWasEnteredSuccessfully)]) {
Expand Down Expand Up @@ -1320,10 +1319,6 @@ - (void)_denyAccess {
[self.delegate respondsToSelector: @selector(maxNumberOfFailedAttemptsReached)]) {
[self.delegate maxNumberOfFailedAttemptsReached];
}
// Or, if you prefer by notifications:
// [[NSNotificationCenter defaultCenter] postNotificationName: @"maxNumberOfFailedAttemptsReached"
// object: self
// userInfo: nil];

if (_failedAttempts == 1) {
_failedAttemptLabel.text =
Expand Down Expand Up @@ -1583,6 +1578,7 @@ - (void)_loadMiscDefaults {
_displayedAsModal = YES;
_hidesBackButton = YES;
_hidesCancelButton = YES;
_passcodeAlreadyExists = YES;
#if !(TARGET_IPHONE_SIMULATOR)
_allowUnlockWithTouchID = [self _allowUnlockWithTouchID];
#else
Expand Down Expand Up @@ -1771,8 +1767,30 @@ - (void)setIfNotEqualTransform:(CGAffineTransform)transform frame:(CGRect)frame
}

- (void)disablePasscodeWhenApplicationEntersBackground {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationWillEnterForegroundNotification
object:nil];
}

- (void)enablePasscodeWhenApplicationEntersBackground {
// To avoid double registering.
[self disablePasscodeWhenApplicationEntersBackground];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}


Expand Down

0 comments on commit 958b322

Please sign in to comment.