Checking Username and Password to match in SQLIte - iphone

How can I check if the Username and Password would be the same as the Username and Password in the Database.
AppDelegate.m:
-(void) readLoginFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the Array
lo = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "Select * from Login";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aLoginName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aLoginPass = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aLoginAccess = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
// Create a new object with the data from the database
Login *los = [[Login alloc] initWithName:aLoginName pass:aLoginPass access:aLoginAccess];
// Add the object to the Array
[lo addObject:los];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
ViewController.m:
-(IBAction)buttonWasClicked:(id)sender
{
if ((userText.text != logins.loginUser) && (passText.text != logins.loginPass))
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:#"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
if (userText.text != logins.loginUser)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
if (passText.text != logins.loginPass)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Even when the Username and Password I type is exactly the same as in the database, it would still show me the error message instead of displaying the next view.

You have to check NSStrings in objective-c with
isEqualToString
For your example:
if (![userText.text isEqualToString:logins.loginUser])
Check: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/isEqualToString:

You can use isEqualToString method to check that two string is equal....
if ([userText.text isEqualToString:logins.loginUser])
and apply condition like this
-(IBAction)buttonWasClicked:(id)sender
{
Login *logins = nil;
logins= [lo objectAtIndexPath:0];
if ([userText.text isEqualToString:logins.loginUser] && [passText.text isEqualToString:logins.loginPass])
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:#"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
else if (![userText.text isEqualToString:logins.loginUser])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if (![passText.text isEqualToString:logins.loginPass])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}

Related

MFMessageController crash sometimes

I have the concept of sharing to contacts in my App and used MFMessageComposeViewController.
-(IBAction)btnAddClicked:(id)sender {
#try {
selections = [[NSMutableArray alloc] init];
for(NSIndexPath *indexPath in arSelectedRows) {
NSMutableDictionary *searchable = [[NSMutableDictionary alloc
]init];
[searchable setObject:[[contactsArray objectAtIndex:indexPath.row]objectForKey:#"Phone"]forKey:#"Phone"];
[selections addObject:searchable];
}
if([selections count]>0)
{
NSString *temp1=#"";
for(int i=0;i<[selections count];i++)
{
toRecepients=[[selections objectAtIndex:i]objectForKey:#"Phone"];
temp1=[temp1 stringByAppendingString:toRecepients];
temp1=[temp1 stringByAppendingString:#","];
}
temp1 = [temp1 substringToIndex:[temp1 length]-1];
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSArray *recipents = [temp1 componentsSeparatedByString:#","];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
messageController.navigationBar.topItem.leftBarButtonItem.title = #"Cancel";
[messageController setRecipients:recipents];
[messageController setBody:self.message];
[self presentModalViewController:messageController animated:YES];
}
else{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Select the contacts you would like to share to" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
}
#catch (NSException *exception) {
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView~iPad" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
} }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result {
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
{
NSString *messa=[NSString stringWithFormat:#"Shared to %lu contact(s)",(unsigned long)[selections count]];
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Succesful" message:messa delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
This is the code Im using, which crashes sometimes and displays the error
Assertion failed: (result == KERN_SUCCESS), function +[XPCMachSendRight wrapSendRight:], file /SourceCache/XPCObjects/XPCObjects-46/XPCMachSendRight.m, line 27.
I have put breakpoint to debug, but xcode doesn't showup where the error is produced.
Any ideas/ suggestions would be appreciable..
Enable zombie Objects to find out the line of actual crash.
You can enable Zombie by following steps:
1.Select you project scheme and chose edit scheme.
2.A window will appear, now select diagnostics.
3.Select check mark for enable Zombie objects.
Now run your project.

How to compare my email text field value to database value in iphone

I have a problem in comparing my textfield email value to the value in sqlite database. If both the values are same then it shows alertbox that "email already exist in database". I have created a function for this purpose named findcontact, I am calling the function from button click. Button click method is working because when I click the button then buttonclick method generate the log which I have written in findContact method.
But problem for me is that how to compare both the email and show alert box if both email matches
- (void) findContact:(NSString *) email{
const char *dbpath = [path UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK){
NSString *querySQL = [NSString stringWithFormat: #"SELECT email FROM CONTACT WHERE email=\"%#\"",email];
NSLog(#" email = %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
NSLog(#" char email = %s", query_stmt);
if (sqlite3_prepare_v2(database, query_stmt, -1, &init_statement, NULL) == SQLITE_OK){
if (sqlite3_step(init_statement) == SQLITE_ROW){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"email already exist in database" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK ", nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"email not exist" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK ", nil];
[alert show];
[alert release];
}
sqlite3_finalize(init_statement);
}
sqlite3_close(database);
}
}
Do comparison like this in your code
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
NSString *emaiID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];
if ([emaiID isEqualToString:yourTextField.text])
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"email already exist in database"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK ", nil];
[alert show];
}
else{
//Your task
}
}
Check this
NSString *querySQL = [NSString stringWithFormat: #"SELECT email FROM CONTACT WHERE email='%#'",email];
if(sqlite3_prepare_v2(sharedDataBase, [querySQL UTF8String], -1, &statment, nil) == SQLITE_OK)
{
if(sqlite3_step(statment)==SQLITE_ROW){
char *email=(char *)sqlite3_column_text(statment, 0);
NSString *emailStr =[NSString stringWithUTF8String:email];
// email exist
}
else{
// email not exist
}
sqlite3_finalize(statment);
}
else
sqlite3_close(sharedDataBase);

How to access or call an action in to a function

I have the right navigation bar button action,I have set an action for it like the following:
-(IBAction)save:(id)sender
{
//Code for saving entered data
UITextField *fieldOne = [self.fields objectAtIndex:0];
UITextField *fieldTwo = [self.fields objectAtIndex:1];
UITextField *fieldThree = [self.fields objectAtIndex:2];
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO reminders(name, event, date) VALUES (\"%#\", \"%#\", \"%#\")", fieldOne.text, fieldTwo.text,fieldThree.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
NSLog(#"%#",fieldOne.text);
NSLog(#"%#",fieldTwo.text);
NSLog(#"%#",fieldThree.text);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"\n Reminder Saved." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Reminder not saved" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
rowID = sqlite3_last_insert_rowid(remindersDB);
NSLog(#"last inserted rowId = %d",rowID);
sqlite3_finalize(statement);
sqlite3_close(remindersDB);
}
//Alert for not entering any data
if ([textField.text length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Fill Data" message:#"Please fill name,event and date of reminder" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
++self.numberOfSaves;
NSLog(#"Number of saves = %d",numberOfSaves);
[self.tableView reloadData];
}
Now I want to check how many times the button is clicked,I have an idea in mind that to create a function and used the condition
-(int)numberOfSaves
{
int saves = 0;
if(self.navigationItem.rightBarButtonItem.enabled==YES)
{
saves++;
}
return saves;
}
It's not working......
Is there anyway to check such condition,If so how can I achieve it,I am newbie to objective-c,please help me out...
Thanks all in advance :)
You cannot return from a void method.
You'd want to change -(void)numberOfSaves to -(int)numberOfSaves, if that were the right way to go about things, but I don't think this is what you want.
In your header file (.h) you'll want to declare an instance variable for the class:
#property (nonatomic, strong) NSInteger numberOfSaves;
In your implementation file (.m) make sure to synthesize it:
#synthesize numberOfSaves = _numberOfSaves;
Replace your IBAction method:
- (IBAction)save:(id)sender
{
++self.numberOfSaves;
}
And just throw away the -(void)numberOfSaves method all together. This should accomplish what I think you're trying to do. You can figure out how many saves you have committed now with self.numberOfSaves

invalide username or password when validate it sqlite iphone

when Click on update button then data does not updated i want to just updated my password
first match old password
if old password match with database then validate new password and conform password using isEquelToString then click on update button and oldpassword must be updated with newPassword
I think user name and password match for single user only there for if it match then it will return one row using following code. Check the following code i think this will work for you.
-(void)checkingDatabase{
databaseName = #"trylogin.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName]retain];
NSLog(#"databse path %#",databasePath);
database = nil;
int myInt = 0;
if(sqlite3_open([databasePath UTF8String], &database) ==SQLITE_OK){
NSString *sql = [[NSString alloc] initWithFormat:#"select COUNT(*) from loginTry where userName='%#' and password='%#'",txtusername.text,txtPassword.text];
const char *sqlStatement = [tempSQL cStringUsingEncoding:NSUTF8StringEncoding];
[tempSQL release];
sqlite3_stmt *compiledStatement;
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
myInt = (int)sqlite3_column_int(compiledStatement, 0);
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
if(myInt ! = 0){
AnimalViewController *FBController = [[AnimalViewController alloc] initWithNibName:#"AnimalViewController" bundle:nil];
[self.view addSubview:FBController.view];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Wrong username or password" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
As above same but some change at condition checking as follows..
if(myInt ! = 0) {
if([newpassword.text isEqualToString: confirmPassword.text]) {
//Write code for update query.
}
else {
//print message new password and confirm password does not match.
}
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Wrong username or password" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}

UIAlert View-for yes/no condition

my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.
I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.
In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.
Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.