2013-04-17 65 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

相關問題