我对iOS开发很新,所以请好好:)我正在开发一个应用程序。 用户将通过填写只是一堆TextFields的表单进行注册。
一旦用户注册,他们将被重定向到他们的主页,其中UIImageView
和UILabel
将显示他们在注册时使用的数据。
现在我的问题是如何确保每次同一用户登录时此UIImageView
和UILabel
都会显示相同的数据。我使用NSUserDefaults
尝试了此操作但是当用户登录时将显示我刚刚注册的用户数据。任何建议都很棒,我也在使用解析数据库。
以下是用户注册时的代码
NSLog(@"Registering user");
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Registering";
PFObject *parent = [PFObject objectWithClassName:@"Parent"];
UIImage *image = form.profilePhoto;
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
PFFile *imageFile = [PFFile fileWithName:@"image.jpeg" data:imageData];
parent[@"Name"] = form.name;
parent[@"Email"] = form.email;
parent[@"Password"] = form.password;
parent[@"Location"] = form.location;
parent[@"ImageFile"] = imageFile;
[parent saveInBackground];
[parent saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[hud hide:YES];
currentUser = YES;
//saveData to parentUser
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:form.name forKey:@"Name"];
//Send user to parent home page
PHomeViewController *lvc=[self.storyboard instantiateViewControllerWithIdentifier:@"Phome"];
SlideNavigationController *navcontroller = [[SlideNavigationController alloc] initWithRootViewController:lvc];
ParentMenu *leftMenu = (ParentMenu*)[self.storyboard instantiateViewControllerWithIdentifier: @"LeftMenuViewController"];
[self.navigationController pushViewController:lvc animated:YES];
navcontroller.leftMenu = leftMenu;
//save data to nsuserdefualts
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UIImage *uploadPhoto = form.profilePhoto;
[defaults setObject:form.name forKey:@"Name"];
[defaults setObject:UIImageJPEGRepresentation(uploadPhoto, 0.6) forKey:@"ImageFile"];
[defaults synchronize];
NSLog(@"Data has been saved");
}
else {
NSLog(@"There was an error in registration");
}
}];
}
}
这是用户登录时运行的代码
(IBAction)登录:(id)发件人{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading...";
PFQuery *query = [PFQuery queryWithClassName:@"Parent"];
[query whereKey:@"Email" equalTo:_Email.text];
[query whereKey:@"Password" equalTo:_Password.text];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (object) {
NSLog(@"User logged in successfully");
//Send user to parent home page
PHomeViewController *lvc=[self.storyboard instantiateViewControllerWithIdentifier:@"Phome"];
SlideNavigationController *navcontroller = [[SlideNavigationController alloc] initWithRootViewController:lvc];
ParentMenu *leftMenu = (ParentMenu*)[self.storyboard instantiateViewControllerWithIdentifier: @"LeftMenuViewController"];
[self.navigationController pushViewController:lvc animated:YES];
navcontroller.leftMenu = leftMenu;
} else {
// LOGIN ERROR
UIAlertView *ALERT = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error logging in please try again later." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[ALERT show];
_Email.text = [NSString stringWithFormat:@""];
_Password.text = [NSString stringWithFormat:@""];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
}];
}
谢谢!