iPhone PassCode Screen
An Example for Pass code screen simple create single view app in
ViewController.h
#import <UIKit/UIKit.h>
typedef enum {
CPLockControllerTypeAuth,
CPLockControllerTypeSet
} CPLockControllerStyle;
@protocol CPLockControllerDelegate<UITextFieldDelegate>
@required
- (void)lockControllerDidFinish:(NSString*)passcode;
- (void)lockControllerDidCancel;
@end
@interface ViewController : UIViewController
{
//Public vars
CPLockControllerStyle style;
NSString *passcode;
NSString *prompt;
NSString *title;
id <CPLockControllerDelegate> delegate;
BOOL hideCode;
//Private vars
BOOL retry;
NSMutableString *tempString;
//UI Elements
UITextField *hiddenField;
IBOutlet UILabel *promptLabel;
IBOutlet UILabel *subPromptLabel;
// UILabel *subPromptLabel;
IBOutlet UITextField *field1;
IBOutlet UITextField *field2;
IBOutlet UITextField *field3;
IBOutlet UITextField *field4;
}
@property (nonatomic, assign) id delegate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic) CPLockControllerStyle style;
@property (nonatomic, retain) NSString *passcode;
@property (nonatomic, retain) NSString *prompt;
@property (nonatomic) BOOL hideCode;
- (void)setTitle:(NSString *)title;
@end
ViewCotroller.m
#import "ViewController.h"
#define kCPLCDefaultSetPrompt @"Enter your new passcode"
#define kCPLCDefaultAuthPrompt @"Enter your passcode"
#define kCPLCDefaultSetTitle @"Set Passcode"
#define kCPLCDefaultConfirmTitle @"Confirm Passcode"
#define kCPLCDefaultAuthTitle @"Enter Passcode"
#define kCPLCDefaultSetError @"Passcodes did not match. Try again."
#define kCPLCDefaultAuthError @"Passcode incorrect. Try again."
@interface ViewController ()
- (void)setupSubviews;
//
//- (void)setupNavigationBar;
- (void)setupTextFields;
- (void)resetFields;
- (void)passcodeDidNotMatch;
- (void)dissmissView;
@property (nonatomic, retain) NSMutableString *tempString;
@property (nonatomic) BOOL retry;
@property (nonatomic, retain) UILabel *promptLabel;
@property (nonatomic, retain) UILabel *subPromptLabel;
@property (nonatomic, retain) UITextField *hiddenField;
//@property (nonatomic, retain) UINavigationItem *navigationItem;
@end
@implementation ViewController
@synthesize delegate,style,passcode,prompt,hiddenField,promptLabel,subPromptLabel,tempString,retry,title,hideCode;
- (id)initWithStyle:(CPLockControllerStyle)theStyle {
if(self = [super init]){
self.style = theStyle;
self.retry = NO;
self.tempString = [NSMutableString string];
}
return self;
}
- (void)viewDidLoad
{//passed default string you can use yours by setting page
passcode = @"1234";
//check if passcode is set for CPLockControllerTypeAuth
if(style == CPLockControllerTypeAuth){
assert(passcode != nil);
}
[self setupSubviews];
// [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)setupSubviews {
//prompt
if(prompt == nil){
if(self.style == CPLockControllerTypeSet){
prompt = kCPLCDefaultSetPrompt;
} else if(self.style == CPLockControllerTypeAuth){
prompt = kCPLCDefaultAuthPrompt;
}
}
//main prompt
promptLabel.text = prompt;
promptLabel.backgroundColor = [UIColor clearColor];
promptLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.50];
promptLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
promptLabel.shadowOffset = CGSizeMake(0, -0.75);
promptLabel.textColor = [UIColor colorWithRed:0.318 green:0.345 blue:0.416 alpha:1.000];
[self.view addSubview:promptLabel];
[self setupTextFields];
}
- (void)setupTextFields {
//create four textfields
field1.backgroundColor = [UIColor whiteColor];
field1.borderStyle = UITextBorderStyleBezel;
//field1.enabled = NO;
field1.secureTextEntry =YES;
field1.tag = 0;
field2.backgroundColor = [UIColor whiteColor];
field2.borderStyle = UITextBorderStyleBezel;
field2.enabled = NO;
field2.secureTextEntry = YES;
field2.tag = 2;
field3.backgroundColor = [UIColor whiteColor];
field3.borderStyle = UITextBorderStyleBezel;
field3.enabled = NO;
field3.secureTextEntry =YES;
field3.tag = 3;
field4.backgroundColor = [UIColor whiteColor];
field4.borderStyle = UITextBorderStyleBezel;
field4.enabled = NO;
field4.secureTextEntry = YES;
field4.tag = 4;
//this is the field the passcode is put into
hiddenField = [[UITextField alloc]initWithFrame:CGRectMake(-3000,-3000,0,0)];
hiddenField.text = @"";
hiddenField.keyboardType = UIKeyboardTypeNumberPad;
[hiddenField becomeFirstResponder];
hiddenField.delegate = self;
[self.view addSubview:hiddenField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int charcount = [textField.text length];
if(self.retry == YES){
charcount-=1;
}
if(charcount == 0){
field1.text = string;
} else if(charcount == 1){
field2.text = string;
} else if(charcount == 2){
field3.text = string;
} else if(charcount == 3){
field4.text = string;
}
[self.tempString appendString:string];
//we've reached 4 chars
if(charcount == 3){
if(self.style == CPLockControllerTypeSet){
if(passcode == nil){
//empty tempstring to passcode string
passcode = [self.tempString copy];
self.tempString = [NSMutableString string];
//reset visible/hidden fields
[self resetFields];
promptLabel.text = kCPLCDefaultConfirmTitle;
self.retry = YES;
} else {
//check if confirm matches first
if([passcode isEqualToString:self.tempString]){
[delegate lockControllerDidFinish:passcode];
[self dissmissView];
//confirm passcode doesn't match
} else {
[self passcodeDidNotMatch];
}
}
} else if(self.style == CPLockControllerTypeAuth){
if([passcode isEqualToString:self.tempString])
{
NSLog(@"Correct Password Show your next window here");
} else {
[self passcodeDidNotMatch];
}
}
}
return YES;
}
- (void)passcodeDidNotMatch {
self.tempString = [NSMutableString string];
if(self.style == CPLockControllerTypeSet){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Encorrect PIN" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
//subPromptLabel.text = kCPLCDefaultSetError;
} else if(self.style == CPLockControllerTypeAuth){
//subPromptLabel.text = kCPLCDefaultAuthError;
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Encorrect PIN" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
self.retry = YES;
[self resetFields];
}
- (void)resetFields {
field1.text = @"";
field2.text = @"";
field3.text = @"";
field4.text = @"";
hiddenField.text = @"";
}
- (void)dissmissView {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)userDidCancel:(id)sender {
//[delegate lockControllerDidCancel];
[self dissmissView];
}
////////////////////////////////////////////
Xib Should be designed with 4 text fields
Thank You.........:)
No comments:
Post a Comment