2013-04-17 61 views
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; 
} 

回答

5

。文本之前和/或之后最有可能存在空格。也许是一两个流浪的换行符。

试试这个:

NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 
NSLog(@"Result = '%@'", serverOutput); // look for space between quotes 
serverOutput = [serverOutput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
+0

非常感谢!这样可行! :) – spatra

+0

顺便说一句 - 你需要正确地逃避用户名和密码,当你把它们添加到URL。否则,使用包含特定字符的用户名或密码的用户会造成问题。另外,我希望你计划在你的最终应用中使用“https”。不要通过普通的http发送纯文本的用户名和密码。 – rmaddy

相关问题