2 // PasscodeLockViewController.m
5 // Created by Mike Mayo on 10/26/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "PasscodeLockViewController.h"
11 #import "UIViewController+Conveniences.h"
12 #import "PasscodeViewController.h"
13 #import "SettingsViewController.h"
16 @implementation PasscodeLockViewController
18 @synthesize settingsViewController;
20 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
21 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
24 - (void)simplePasscodeSwitchChanged:(id)sender {
25 // no need to authenticate for this one
26 simplePasscodeOn = simplePasscodeSwitch.on;
27 if (simplePasscodeOn) {
28 [Keychain setString:@"YES" forKey:@"passcode_lock_simple_passcode_on"];
30 [Keychain setString:@"NO" forKey:@"passcode_lock_simple_passcode_on"];
34 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
35 if (buttonIndex == 0) {
37 [Keychain setString:@"YES" forKey:@"passcode_lock_erase_data_on"];
40 [Keychain setString:@"NO" forKey:@"passcode_lock_erase_data_on"];
42 [eraseDataSwitch setOn:eraseDataOn animated:YES];
45 - (void)eraseDataSwitchChanged:(id)sender {
46 if (eraseDataSwitch.on) {
47 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"All data in this app will be erased after 10 failed passcode attempts." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Enable" otherButtonTitles:nil];
48 [sheet showInView:self.view];
52 [Keychain setString:@"NO" forKey:@"passcode_lock_erase_data_on"];
57 #pragma mark View lifecycle
61 self.navigationItem.title = @"Passcode Lock";
63 simplePasscodeSwitch = [[UISwitch alloc] init];
64 [simplePasscodeSwitch addTarget:self action:@selector(simplePasscodeSwitchChanged:) forControlEvents:UIControlEventValueChanged];
66 eraseDataSwitch = [[UISwitch alloc] init];
67 [eraseDataSwitch addTarget:self action:@selector(eraseDataSwitchChanged:) forControlEvents:UIControlEventValueChanged];
71 - (void)viewWillAppear:(BOOL)animated {
72 [super viewWillAppear:animated];
73 passcodeLockOn = [[Keychain getStringForKey:@"passcode_lock_passcode_on"] isEqualToString:@"YES"];
74 simplePasscodeOn = [[Keychain getStringForKey:@"passcode_lock_simple_passcode_on"] isEqualToString:@"YES"];
75 eraseDataOn = [[Keychain getStringForKey:@"passcode_lock_erase_data_on"] isEqualToString:@"YES"];
76 simplePasscodeSwitch.on = simplePasscodeOn;
77 eraseDataSwitch.on = eraseDataOn;
81 - (void)viewDidAppear:(BOOL)animated {
82 [super viewDidAppear:animated];
86 - (void)viewWillDisappear:(BOOL)animated {
87 [super viewWillDisappear:animated];
91 - (void)viewDidDisappear:(BOOL)animated {
92 [super viewDidDisappear:animated];
97 #pragma mark Table view data source
99 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
100 // Return the number of sections.
105 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
106 // Return the number of rows in the section.
110 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
112 return @"A simple passcode is a 4 digit number.";
113 } else if (section == 3) {
114 return @"Erase all data in this app after 10 failed passcode attempts.";
120 // Customize the appearance of table view cells.
121 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
123 static NSString *CellIdentifier = @"Cell";
125 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
127 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
130 // Configure the cell...
131 if (indexPath.section == 0) {
132 if (passcodeLockOn) {
133 cell.textLabel.text = @"Turn Passcode Off";
135 cell.textLabel.text = @"Turn Passcode On";
137 cell.textLabel.textColor = [UIColor blackColor];
138 cell.textLabel.textAlignment = UITextAlignmentCenter;
139 cell.accessoryView = nil;
140 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
141 } else if (indexPath.section == 1) {
142 cell.textLabel.text = @"Change Passcode";
143 if (passcodeLockOn) {
144 cell.textLabel.textColor = [UIColor blackColor];
145 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
147 cell.textLabel.textColor = [UIColor grayColor];
148 cell.selectionStyle = UITableViewCellSelectionStyleNone;
150 cell.textLabel.textAlignment = UITextAlignmentCenter;
151 cell.accessoryView = nil;
152 } else if (indexPath.section == 2) {
153 cell.textLabel.text = @"Simple Passcode";
154 cell.textLabel.textColor = [UIColor blackColor];
155 cell.textLabel.textAlignment = UITextAlignmentLeft;
156 cell.accessoryView = simplePasscodeSwitch;
157 cell.selectionStyle = UITableViewCellSelectionStyleNone;
158 if (passcodeLockOn) {
159 cell.textLabel.textColor = [UIColor grayColor];
160 simplePasscodeSwitch.enabled = NO;
162 cell.textLabel.textColor = [UIColor blackColor];
163 simplePasscodeSwitch.enabled = YES;
165 } else if (indexPath.section == 3) {
166 cell.textLabel.text = @"Erase Data";
167 cell.textLabel.textAlignment = UITextAlignmentLeft;
168 cell.accessoryView = eraseDataSwitch;
169 cell.selectionStyle = UITableViewCellSelectionStyleNone;
170 if (passcodeLockOn) {
171 cell.textLabel.textColor = [UIColor blackColor];
172 eraseDataSwitch.enabled = YES;
174 cell.textLabel.textColor = [UIColor grayColor];
175 eraseDataSwitch.enabled = NO;
183 #pragma mark Table view delegate
185 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
186 // Navigation logic may go here. Create and push another view controller.
187 if (indexPath.section == 0) {
188 PasscodeViewController *vc = [[PasscodeViewController alloc] initWithNibName:@"PasscodeViewController" bundle:nil];
189 if (passcodeLockOn) {
190 vc.mode = kModeDisablePasscode;
192 vc.mode = kModeSetPasscode;
194 vc.settingsViewController = self.settingsViewController;
195 vc.passcodeLockViewController = self;
196 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
197 vc.modalPresentationStyle = UIModalPresentationFormSheet;
199 [self presentModalViewControllerWithNavigation:vc];
201 } else if (indexPath.section == 1 && passcodeLockOn) {
202 PasscodeViewController *vc = [[PasscodeViewController alloc] initWithNibName:@"PasscodeViewController" bundle:nil];
203 vc.mode = kModeChangePasscode;
204 vc.settingsViewController = self.settingsViewController;
205 vc.passcodeLockViewController = self;
206 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
207 vc.modalPresentationStyle = UIModalPresentationFormSheet;
209 [self presentModalViewControllerWithNavigation:vc];
216 #pragma mark Memory management
218 - (void)didReceiveMemoryWarning {
219 // Releases the view if it doesn't have a superview.
220 [super didReceiveMemoryWarning];
222 // Relinquish ownership any cached data, images, etc that aren't in use.
225 - (void)viewDidUnload {
226 // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
227 // For example: self.myOutlet = nil;
232 [simplePasscodeSwitch release];
233 [eraseDataSwitch release];
234 [settingsViewController release];