objective-c 實現用戶驗證,登陸 Xcode iOS


- (void)viewDidLoad
{
    [super viewDidLoad];
 //加載窗口的時候把從文件里讀出用戶名。
 NSString *filePath = [self documentsPath:@"user.txt"]; 
    //讀出文件存到數組username中
    NSArray *username = [NSArray arrayWithContentsOfFile:filePath];
    self.TXF1.text   = [username objectAtIndex:0];
    self.Txtpwd.text = [username objectAtIndex:1];
    //文本顯示設為安全。星號
    self.Txtpwd.secureTextEntry = YES;
}
//登陸驗證 - (IBAction)loginpage:(id)sender { NSLog(@"login...\n"); NSString *filePath = [self documentsPath:@"user.txt"]; //從user這個文件里讀出用戶名和密碼是否與輸入的相同 NSArray *username = [NSArray arrayWithContentsOfFile:filePath]; if([TXF1.text isEqualToString:[username objectAtIndex:0]] && [Txtpwd.text isEqualToString:[username objectAtIndex:1]]) { //如果驗證正確,則重新打開一個窗口 if(self.loginhome ==nil) { NSLog(@"loginhome"); loginhome *homepage = [[loginhome alloc]initWithNibName:@"loginhome" bundle:nil]; self.loginhome = [homepage autorelease]; [self.view addSubview:self.loginhome.view]; } else { [self.view addSubview:self.loginhome.view]; } NSLog(@"登陸成功!\n"); } else { NSLog(@"用戶名或密碼錯誤!\n"); judgelogin = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用戶名或密碼錯誤!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil]; [judgelogin show]; [judgelogin release]; } } //讀程序目錄而准備 -(NSString *)bundlePath:(NSString *)fileName { return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName]; } -(NSString *)documentsPath:(NSString *)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:fileName]; } -(NSString *)documentsPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return documentsDirectory; }




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM