Returning "0" everytime when fetching keychain saved passcode from keychain access for ios app
Asked Answered
P

1

6

I am using KeychainItemWrapper class(integrated .h and .m file in project) for saving passcode in Keychain for iOS App. Also imported "Security" Framework and "keychianItemWrapper.h" class in the project wherever it is needed.(#import , #import "KeychainItemWrapper.h" )

I am using below code in app delegate method for saving passcode in keychain access :

if([textfieldPassword1.text isEqual:textfieldPassword2.text]){

                NSLog(@"CONGRATS !! PASSCODE MATCHED !!!");           

                //converting "textfieldPassword1" to NSNumber
                NSNumber *textfieldPasscode1Num = [NSNumber numberWithInt:[textfieldPassword1.text intValue]];

                //saving passcode to the keychain access  
               [keychain setObject:textfieldPasscode1Num forKey:(__bridge id)kSecValueData];

           //    [keychain setObject:[NSNumber numberWithInt:[textfieldPassword1.text intValue]] forKey:(__bridge id)kSecAttrAccount];

                // if passcode matches then load Show Lock Screen Page
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                self.viewController = [[ViewController alloc] init];
                [self.window addSubview:self.viewController.view];
                [self.window makeKeyAndVisible];
            }

AND HERE AM RESETTING PASSCODE USING BELOW CODE :

if(_isResetPasscode){
                NSLog(@"Code here for Update Passcode in Keychain Access !!!");
                _isResetPasscode = FALSE;

              // "keychain" is object of "KeychainItemWrapper" class         
                [keychain resetKeychainItem];

/* Again setting the new passcode entered by user in keychain access.. IT IS NOT SAVING IN keychain access, where above the same line of code was working for saving passcode in keychain access
/*
                [keychain setObject:resetPasscodeNum forKey:(__bridge id)kSecValueData];

                NSLog(@"----Passcode Re-Setted ----!! %@\n",resetPasscodeNum);
            }

When I am printing keychain passcode in console every time it is printing "0". Please guide me where i am doing wrong... Your help would be appreciated !!

Thanks in Advance

Portwine answered 23/8, 2012 at 18:28 Comment(0)
D
0

You haven't specified what format the entered password could be. I am assuming it could be any sort of text. If that is the case, then this line will fail if the entered text is not actually a number:

[textfieldPassword1.text intValue]

and will return 0;

Danyel answered 23/8, 2012 at 18:38 Comment(1)
Hi @Danyel Entered passcode is "Number" Input, see code am using "UIKeyboardTypeNumberPad" for passcode : UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"Enter Passcode" message:@"Please Enter 4 digit Passcode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil]; textfieldPassword1 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)]; textfieldPassword1.keyboardType = UIKeyboardTypeNumberPad; [textfieldPassword1 setDelegate:self]; [alertView addSubview:textfieldPassword1]; [alertView show];Portwine

© 2022 - 2024 — McMap. All rights reserved.