Objective-C中如何通过Plist验证UITextField用户输入并实现登录跳转
实现Objective-C登录验证(Plist数据源)与页面跳转
Hey,我来帮你一步步完成这个需求,从Plist读取用户数据、验证登录到跳转个人资料页的全流程都给你理清楚:
一、先准备好存储用户数据的Plist文件
- 在Xcode里右键项目 → 选择「New File」→ 找到「Property List」创建,命名为
UserData.plist - 打开这个Plist,把根类型改成
Dictionary,然后添加两个String类型的键值对:- Key:
username,Value: 你要测试的用户名(比如"demoUser") - Key:
password,Value: 对应的测试密码(比如"demoPass123")
注意:确保这个Plist已经添加到你的Target里,不然运行时会找不到文件
- Key:
二、搭建登录页面的UI与关联代码
假设你的登录页面ViewController是LoginViewController:
- 在
.h文件里定义控件的IBOutlet和按钮的IBAction:
#import <UIKit/UIKit.h> @interface LoginViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *usernameTF; @property (weak, nonatomic) IBOutlet UITextField *passwordTF; - (IBAction)submitBtnTapped:(UIButton *)sender; @end
- 打开Storyboard,拖入两个
UITextField(分别用于用户名、密码输入)和一个UIButton(提交按钮),把它们分别关联到上面的IBOutlet和IBAction。
三、核心逻辑:读取Plist并验证登录
在LoginViewController.m里实现验证和跳转的代码:
#import "LoginViewController.h" #import "ProfileViewController.h" // 提前引入个人资料页的头文件 @implementation LoginViewController - (IBAction)submitBtnTapped:(UIButton *)sender { // 1. 获取用户输入的内容 NSString *inputUser = self.usernameTF.text; NSString *inputPwd = self.passwordTF.text; // 先判断是否为空输入 if (inputUser.length == 0 || inputPwd.length == 0) { UIAlertController *emptyAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名和密码不能为空" preferredStyle:UIAlertControllerStyleAlert]; [emptyAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:emptyAlert animated:YES completion:nil]; return; } // 2. 读取Plist里的存储数据 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"UserData" ofType:@"plist"]; NSDictionary *storedData = [NSDictionary dictionaryWithContentsOfFile:plistPath]; NSString *storedUser = storedData[@"username"]; NSString *storedPwd = storedData[@"password"]; // 3. 对比验证 if ([inputUser isEqualToString:storedUser] && [inputPwd isEqualToString:storedPwd]) { // 登录成功,先弹提示,再跳转 UIAlertController *successAlert = [UIAlertController alertControllerWithTitle:@"成功" message:@"登录成功!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 跳转到个人资料页 ProfileViewController *profileVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfileVC"]; // 把用户名传递过去展示 profileVC.currentUsername = storedUser; [self.navigationController pushViewController:profileVC animated:YES]; }]; [successAlert addAction:okAction]; [self presentViewController:successAlert animated:YES completion:nil]; } else { // 验证失败 UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"错误" message:@"用户名或密码不正确" preferredStyle:UIAlertControllerStyleAlert]; [failAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:failAlert animated:YES completion:nil]; } } // 点击空白处收起键盘,提升用户体验 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } @end
四、个人资料页的实现
创建ProfileViewController来展示用户信息:
- 在
.h文件里定义接收用户名的属性:
#import <UIKit/UIKit.h> @interface ProfileViewController : UIViewController @property (nonatomic, strong) NSString *currentUsername; @property (weak, nonatomic) IBOutlet UILabel *userInfoLabel; // 展示用户名的Label @end
- 在
.m文件里设置页面内容:
#import "ProfileViewController.h" @implementation ProfileViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"个人资料"; // 把用户名展示到Label上 self.userInfoLabel.text = [NSString stringWithFormat:@"当前用户: %@", self.currentUsername]; } @end
别忘了在Storyboard里给
ProfileViewController设置Storyboard ID为ProfileVC,并把Label关联到userInfoLabel。
重要提示
- 确保你的登录页面已经嵌入了
UINavigationController,不然pushViewController会无效(在Storyboard里选中LoginVC → 顶部菜单Editor → Embed In → Navigation Controller) - 这个示例是明文存储密码,正式项目绝对不能这么做,建议用Keychain或者加密算法存储密码哈希值
内容的提问来源于stack exchange,提问作者Suresh M460
相关产品推荐
相关产品推荐

