-1

i have used Core Data in my App to save data of Users. A single User have "Username","Password","Confirm Password","Mobile Number".

I am able to save Registered users information and also i am able to do validation of username and password and login to the Application . Now i want to change My "Password" if the user entered "Username","Mobile Number" correctly. How can i do this ?

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Suresh Peddisetti
  • 3,782
  • 3
  • 23
  • 26

4 Answers4

3

It is also same as save registered user information,

// Set password and save
[user setPassword:newPassword];
[user.managedObjectContext save:nil];
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
2

After retrieving the Object from core data you use a predicate to update the object then save it.

ex:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Username = 'YourUserName' AND MobileNumber = 'TheMobileNumber'"];
NSFetchRequest *request = [NSFetchRequest new];
//setup request here
[request setPredicate: predicate];

NSArray *result = [ManagedObjectContext executeFetchRequest:request error:&errors]
YourObject *object = (YourObject *)[result lastObject]; // assuming you only have one entry get the last object in the array or the only object.
object.password = "your desired password";
[ManagedObjectContext save:&error];
Joshua
  • 2,432
  • 1
  • 20
  • 29
1

update User information into DB

 NSEntityDescription *productEntity=[NSEntityDescription entityForName:@"Registration" inManagedObjectContext:context];


        NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
        [fetch setEntity:productEntity];
        NSPredicate *p=[NSPredicate predicateWithFormat:@"username == %@ AND mail_no == %@",user-username,mobile_no];
        [fetch setPredicate:p];
        //... add sorts if you want them
        NSError *fetchError;
        NSArray *fetchedProducts=[context executeFetchRequest:fetch error:&fetchError];

        NSLog(@" update fetch data %@", fetchedProducts);

        for (NSManagedObject *record in fetchedProducts) {

            [record setValue:_mailTextField.text forKey:@"mailID"];

        }
codercat
  • 22,873
  • 9
  • 61
  • 85
0

This is the code what i Wrote to Change Password. I change my Password by Using "Username" and "Oldpassword". it's Working Fine.

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username like %@ and password like %@",username,self.oldPass.text];
[request setPredicate:predicate];

NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];

if(matchingData.count<=0)
  {
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Old Password Does not Match" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
      [alert show];
  }
else
  {
      for(NSManagedObject *obj in matchingData)
      {
          [obj setValue:changedPass.text forKey:@"password"];
          [obj setValue:confirmChangedPass.text forKey:@"confirmpassword"];
      }
          [context save:&error];
          alert1 = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Password Changed Successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
          [alert1 show];
   }
}
Suresh Peddisetti
  • 3,782
  • 3
  • 23
  • 26