Skip to content

Commit a59ab99

Browse files
committed
Added incorrect password logic to settings
1 parent 636b202 commit a59ab99

File tree

4 files changed

+107
-8
lines changed

4 files changed

+107
-8
lines changed

TOPasscodeViewController/TOPasscodeSettingsViewController.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ NS_ASSUME_NONNULL_BEGIN
2121

2222
@protocol TOPasscodeSettingsViewControllerDelegate <NSObject>
2323

24+
@optional
25+
2426
/** Called when the user was prompted to input their current passcode.
2527
Return YES if passcode was right and NO otherwise.
2628
2729
Returning NO will cause a warning label to appear
2830
*/
29-
- (BOOL)passcodeSettingsViewControllerDidAttemptCurrentPasscode:(NSString *)passcode;
31+
- (BOOL)passcodeSettingsViewController:(TOPasscodeSettingsViewController *)passcodeSettingsViewController
32+
didAttemptCurrentPasscode:(NSString *)passcode;
3033

3134
/** Called when the user has successfully set a new passcode. At this point, you should save over
3235
the old passcode with the new one. */
33-
- (void)passcodeSettingsViewControllerDidChangeToNewPasscode:(NSString *)passcode ofType:(TOPasscodeType)type;
36+
- (void)passcodeSettingsViewController:(TOPasscodeSettingsViewController *)passcodeSettingsViewController
37+
didChangeToNewPasscode:(NSString *)passcode ofType:(TOPasscodeType)type;
3438

3539
@end
3640

@@ -41,10 +45,16 @@ NS_ASSUME_NONNULL_BEGIN
4145
/** Delegate event for controlling and responding to the behavior of this controller */
4246
@property (nonatomic, weak, nullable) id<TOPasscodeSettingsViewControllerDelegate> delegate;
4347

48+
/** The current state of the controller (confirming old passcode or creating a new one) */
49+
@property (nonatomic, assign) TOPasscodeSettingsViewState state;
50+
4451
/** Set the visual style of the view controller (light or dark) */
4552
@property (nonatomic, assign) TOPasscodeSettingsViewStyle style;
4653

47-
/** Before setting a new passcode, show a UI to validate the existing password. (Default is YES) */
54+
/** The number of incorrect passcode attempts the user has made. Use this property to decide when to disable input. */
55+
@property (nonatomic, assign) NSInteger failedPasscodeAttemptCount;
56+
57+
/** Before setting a new passcode, show a UI to validate the existing password. (Default is NO) */
4858
@property (nonatomic, assign) BOOL requireCurrentPasscode;
4959

5060
/** If set, the view controller will disable input until this date time has been reached */

TOPasscodeViewController/TOPasscodeSettingsViewController.m

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
@interface TOPasscodeSettingsViewController ()
2020

21+
// Views
2122
@property (nonatomic, strong) UIView *containerView;
2223
@property (nonatomic, strong) UILabel *titleLabel;
2324
@property (nonatomic, strong) TOPasscodeNumberInputView *numberInputView;
@@ -32,11 +33,26 @@ - (instancetype)initWithStyle:(TOPasscodeSettingsViewStyle)style
3233
{
3334
if (self = [self initWithNibName:nil bundle:nil]) {
3435
_style = style;
36+
[self setUp];
3537
}
3638

3739
return self;
3840
}
3941

42+
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
43+
{
44+
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
45+
[self setUp];
46+
}
47+
48+
return self;
49+
}
50+
51+
- (void)setUp
52+
{
53+
_failedPasscodeAttemptCount = 0;
54+
}
55+
4056
- (void)viewDidLoad {
4157
[super viewDidLoad];
4258

@@ -64,6 +80,7 @@ - (void)viewDidLoad {
6480
self.numberInputView = [[TOPasscodeNumberInputView alloc] initWithRequiredLength:4];
6581
self.numberInputView.tintColor = [UIColor blackColor];
6682
self.numberInputView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
83+
self.numberInputView.passcodeCompletedHandler = ^(NSString *passcode) { [weakSelf numberViewDidEnterPasscode:passcode]; };
6784
[self.numberInputView sizeToFit];
6885
[self.containerView addSubview:self.numberInputView];
6986

@@ -76,10 +93,11 @@ - (void)viewDidLoad {
7693
self.warningLabel = [[TOPasscodeSettingsWarningLabel alloc] initWithFrame:CGRectZero];
7794
self.warningLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
7895
self.warningLabel.hidden = YES;
96+
[self.warningLabel sizeToFit];
7997
[self.containerView addSubview:self.warningLabel];
8098

8199
// Add callbacks for the keypad view
82-
self.keypadView.numberButtonTappedHandler= ^(NSInteger number) {
100+
self.keypadView.numberButtonTappedHandler = ^(NSInteger number) {
83101
NSString *numberString = [NSString stringWithFormat:@"%ld", number];
84102
[weakSelf.numberInputView appendPasscodeCharacters:numberString animated:NO];
85103
};
@@ -114,6 +132,38 @@ - (void)viewDidLoad {
114132
[self applyThemeForStyle:self.style];
115133
}
116134

135+
- (void)viewWillAppear:(BOOL)animated
136+
{
137+
[super viewWillAppear:animated];
138+
139+
self.state = self.requireCurrentPasscode ? TOPasscodeSettingsViewStateEnterCurrentPassword : TOPasscodeSettingsViewStateEnterNewPassword;
140+
[self updateViewsForState:self.state];
141+
}
142+
143+
- (void)updateViewsForState:(TOPasscodeSettingsViewState)state
144+
{
145+
BOOL confirmingPasscode = state == TOPasscodeSettingsViewStateEnterCurrentPassword;
146+
147+
self.warningLabel.hidden = (confirmingPasscode && self.failedPasscodeAttemptCount == 0);
148+
self.warningLabel.numberOfWarnings = self.failedPasscodeAttemptCount;
149+
150+
CGRect frame = self.warningLabel.frame;
151+
frame.origin.x = (CGRectGetWidth(self.view.frame) - frame.size.width) * 0.5f;
152+
self.warningLabel.frame = frame;
153+
154+
switch (state) {
155+
case TOPasscodeSettingsViewStateEnterCurrentPassword:
156+
self.titleLabel.text = NSLocalizedString(@"Enter your passcode", @"");
157+
break;
158+
case TOPasscodeSettingsViewStateEnterNewPassword:
159+
self.titleLabel.text = NSLocalizedString(@"Enter a new passcode", @"");
160+
break;
161+
case TOPasscodeSettingsViewStateConfirmNewPassword:
162+
self.titleLabel.text = NSLocalizedString(@"Confirm new passcode", @"");
163+
break;
164+
}
165+
}
166+
117167
- (void)viewDidLayoutSubviews
118168
{
119169
[super viewDidLayoutSubviews];
@@ -178,4 +228,20 @@ - (void)applyThemeForStyle:(TOPasscodeSettingsViewStyle)style
178228
}
179229
}
180230

231+
#pragma mark - Data Management -
232+
- (void)numberViewDidEnterPasscode:(NSString *)passcode
233+
{
234+
if (![self.delegate respondsToSelector:@selector(passcodeSettingsViewController:didAttemptCurrentPasscode:)]) {
235+
return;
236+
}
237+
238+
BOOL correct = [self.delegate passcodeSettingsViewController:self didAttemptCurrentPasscode:passcode];
239+
if (!correct) {
240+
self.failedPasscodeAttemptCount++;
241+
[self.numberInputView resetPasscodeAnimated:YES playImpact:YES];
242+
}
243+
244+
[self updateViewsForState:self.state];
245+
}
246+
181247
@end

TOPasscodeViewController/Views/TOPasscodeSettingsWarningLabel.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ - (void)sizeToFit
5757
[super sizeToFit];
5858
[self.label sizeToFit];
5959

60-
CGRect frame = self.frame;
6160
CGRect labelFrame = self.label.frame;
61+
CGRect frame = self.frame;
6262

63+
CGPoint midPoint = (CGPoint){CGRectGetMidX(frame), CGRectGetMidY(frame)};
6364
labelFrame = CGRectInset(labelFrame, -self.textPadding.width, -self.textPadding.height);
6465
frame.size = labelFrame.size;
65-
frame.origin.x = CGRectGetMidX(frame) - (CGRectGetWidth(labelFrame) * 0.5f);
66-
frame.origin.y = CGRectGetMidY(frame) - (CGRectGetHeight(labelFrame) * 0.5f);
6766
self.frame = frame;
6867
}
6968

@@ -131,4 +130,12 @@ + (UIImage *)roundedBackgroundImageWithHeight:(CGFloat)height
131130
return image;
132131
}
133132

133+
#pragma mark - Accessors -
134+
135+
- (void)setNumberOfWarnings:(NSInteger)numberOfWarnings
136+
{
137+
_numberOfWarnings = numberOfWarnings;
138+
[self setTextForCount:_numberOfWarnings];
139+
}
140+
134141
@end

TOPasscodeViewControllerExample/SettingsViewController.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#import "SettingsViewController.h"
1010
#import "TOPasscodeSettingsViewController.h"
1111

12-
@interface SettingsViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
12+
@interface SettingsViewController () <UIImagePickerControllerDelegate,
13+
UINavigationControllerDelegate,
14+
TOPasscodeSettingsViewControllerDelegate>
1315

1416
@property (nonatomic, strong) UIImageView *imageView;
1517

@@ -49,6 +51,18 @@ - (void)doneButtonTapped
4951
}
5052
}
5153

54+
#pragma mark - Settings Controller Delegate -
55+
56+
- (BOOL)passcodeSettingsViewController:(TOPasscodeSettingsViewController *)passcodeSettingsViewController didAttemptCurrentPasscode:(NSString *)passcode
57+
{
58+
return [passcode isEqualToString:self.passcode];
59+
}
60+
61+
- (void)passcodeSettingsViewController:(TOPasscodeSettingsViewController *)passcodeSettingsViewController didChangeToNewPasscode:(NSString *)passcode ofType:(TOPasscodeType)type
62+
{
63+
64+
}
65+
5266
#pragma mark - Table view data source
5367

5468
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
@@ -132,6 +146,8 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
132146
}
133147
else if (indexPath.section == 0) {
134148
TOPasscodeSettingsViewController *settingsController = [[TOPasscodeSettingsViewController alloc] init];
149+
settingsController.delegate = self;
150+
settingsController.requireCurrentPasscode = YES;
135151
[self.navigationController pushViewController:settingsController animated:YES];
136152
}
137153
else {

0 commit comments

Comments
 (0)