Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
POKE !
88 PointsChange password + ribbit
How do you let the users change the password, username etc in the Ribbit app. Any ideas? Kisses
2 Answers
Stone Preston
42,016 Pointsyou can change the password of the current user and then resave the user object
[PFUser currentUser].password = someTextField.text;
[[PFUser currentUser] saveInBackground];
to implement this you would just need to have a settings button or something similar that brings up a view that lets them type their new password in a text field and then run the code I posted above when the user presses save.
POKE !
88 PointsNice. How do I make the current username and password appear before I change it?
This is my code now
<p>
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import <QuartzCore/QuartzCore.h>
@interface SettingsUPEViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
//@property (strong, nonatomic) IBOutlet UITextField *password2Field;
@property (strong, nonatomic) IBOutlet UITextField *mailField;
- (IBAction)updateSave:(id)sender;
@end
#import "SettingsUPEViewController.h"
@interface SettingsUPEViewController ()
@end
@implementation SettingsUPEViewController
NSArray *retriveFormParse;
NSArray *profileArray;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
profileArray = [[NSArray alloc] initWithArray:objects];
// The find succeeded.
NSLog(@"@%", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", objects);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
PFObject *object = [query getFirstObject]; // synchronous request, not ideal, look at getFirstObjectInBackgroundWithBlock
_usernameField.text = [object valueForKey:@"newname"];
_passwordField.text = [object valueForKey:@"newpassword"];
_mailField.text = [object valueForKey:@"newmail"];
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
_usernameField.layer.masksToBounds=YES;
_usernameField.layer.borderColor=[[UIColor whiteColor]CGColor];
_usernameField.layer.borderWidth= 1.0f;
_passwordField.layer.masksToBounds=YES;
_passwordField.layer.borderColor=[[UIColor whiteColor]CGColor];
_passwordField.layer.borderWidth= 1.0f;
//_password2Field.layer.masksToBounds=YES;
//_password2Field.layer.borderColor=[[UIColor whiteColor]CGColor];
//_password2Field.layer.borderWidth= 1.0f;
_mailField.layer.masksToBounds=YES;
_mailField.layer.borderColor=[[UIColor whiteColor]CGColor];
_mailField.layer.borderWidth= 1.0f;
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 18) ? NO : YES;
}
- (void) retriveFromParse {
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"@%", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", objects);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (IBAction)updateSave:(id)sender {
// Create PFObject with recipe information
PFUser *profile = [PFUser currentUser];
[profile setObject:_usernameField.text forKey:@"newname"];
[profile setObject:_passwordField.text forKey:@"newpassword"];
[profile setObject:_mailField.text forKey:@"newmail"];
// Upload recipe to Parse
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Complete" message:@"Successfully saved profile" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
// Notify table view to reload the profile from Parse cloud
// Dismiss the controller
// [self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"nameSet" sender:self];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
/// if(![oldPassword.text isEqualToString:[user password]]){
/// [alertMessagePassword setMessage:@"The old password is incorrect!"];
/// [alertMessagePassword show];
#pragma mark - Textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
</p>
```
Stone Preston
42,016 Pointsim not sure if you can show the current users password as that field is not readable, only writable.