1
我想在XCode中为iPhone应用程序的登录过程进行编码。问题出在下面的NSString serverOutput上。当我使用printf(serverOutput.UTF8String)打印它时;它向控制台打印“是”。但是,当我比较serverOutput到“是”它不起作用。任何帮助,将不胜感激。这里是我的代码:基于帮助他人有类似的情况,我会说的问题是,从服务器的响应不只是字符串"Yes"
为什么isEqualToString不适用于NSString?
- (IBAction) loginButton: (id) sender
{
// TODO: spawn a login thread
indicator.hidden = FALSE;
[indicator startAnimating];
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",userName.text, password.text];
NSString *hostStr = @"http://10.243.1.184/connectDB.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
printf(serverOutput.UTF8String);
if([serverOutput isEqualToString:@"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect"
delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
//[self.navigationController pushViewController:DashboardViewController animated:YES];
loginbutton.enabled = TRUE;
}
loginbutton.enabled = FALSE;
}
非常感谢!这样可行! :) – spatra
顺便说一句 - 你需要正确地逃避用户名和密码,当你把它们添加到URL。否则,使用包含特定字符的用户名或密码的用户会造成问题。另外,我希望你计划在你的最终应用中使用“https”。不要通过普通的http发送纯文本的用户名和密码。 – rmaddy