iOS学习(三十二)简单的登陆界面保存用户名到home,下次启动并显示出来

时间:2021-03-04 21:32:17

简单的登陆界面保存用户名到home,下次启动并显示出来

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取home路径
NSString * path = NSHomeDirectory();
//在home路径下创建save.txt文件
path= [path stringByAppendingPathComponent:@"save.txt"];
NSLog(@"path= %@",path);
//获取home路径下save.txt文件的uname值
NSString * uname = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if(uname==nil){
NSLog(@"你没有保存过内容");
}
//输入框获取到的uname的值显示到界面上
self.uname.text = uname;

}

//点击登陆界面
- (IBAction)login:(id)sender {
NSString * str = self.uname.text;
if(str.length>0){
NSString * path = NSHomeDirectory();
path =[path stringByAppendingPathComponent:@"save.txt"];
//保存输入框输入的值到save.txt文件
[str writeToFile:path atomically:YES];
}

}