The following code works almost perfectly. I am connecting to a mysql server on my localhost from Xcode using php in the middle. This in the end will be a login system:
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Check.php?user=%@&pass=%@",txtName.text,passName.text];
// to execute php code
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
// to receive the returned value
NSString *strResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"%@",strResult);
NSString *success = @"Success";
if ([strResult isEqualToString:success]) {
NSLog(@"I realize that the two strings are the same");
}
else {
NSLog(@"The two strings are not the same");
}
The strResult prints out in the debugger the following items that I am telling the php file to echo back to me for the different conditions, (if the username and password is right or wrong)
However, for some reason the if statement part of the code is always going to the else method even though in the output it specifically says that the string, strResult, is containing the word “Success”.
This is so irritating because I can see that both strings, (strResult and success), are equal to each other but for some reason Xcode cannot.
Your
strResultmight contain whitespace at the end. Try logging like this to get a hex dump of the characters in the string:OR
If it is a whitespace problem, you can trim it using the answer here: What's the best way to trim whitespace from a string in Cocoa Touch?