views:

842

answers:

3

hi......i am new in iphone programming......i want to do an activation module which will send a http request with a PIN numbr and then read the response...if the response is "OK" it opens up with a main menu...........the problem is that i am recieving the response as "OK" but i am unable to compare it with a NSString @"OK"............so how to compare the http response with a string...........please give ur suggestions...thanks.

Here is my piece of code......

-(IBAction) submitPINAction:(id) sender { printf("inside submit btn"); mydata = [NSMutableData alloc]; NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://192.168.100.3/WWTF/activationApp.php?PIN=11111"]

            cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
 mydata = [[NSMutableData data] retain];
}
else
{
 //Handle error : Not received data
 printf("No internet connection");
}

}

-(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { printf("data received"); if (mydata==nil) { mydata = [[NSMutableData alloc] initWithCapacity:2048]; } [mydata appendData:incrementalData]; NSString *temp = [[NSString alloc] initWithData:mydata encoding:NSASCIIStringEncoding];

NSString *string1 = @"OK";


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if([string1 isEqualToString:temp]){   // HERE IS THE PROBLEM, THE STRINGS ARE NOT GETTIN COMPARED

 [prefs setBool:TRUE  forKey: @"activationKey"];
 // show the main menu
 mainMenuController *mmC = [[mainMenuController alloc]initWithNibName:@"mainMenu" bundle:[NSBundle mainBundle]];
 self.mmainMenuController = mmC;
 [mmC release];
 [self.view addSubview:[mmainMenuController view]];
}
else{
 printf("in else");
 [prefs setBool:FALSE  forKey: @"activationKey"];
 //show an alert
 UIAlertView *alertActivationFail = [[UIAlertView alloc] initWithTitle:@"Activation Failed!" message:@"PIN is Incorrect" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alertActivationFail show];
 [alertActivationFail release];
}

}


+5  A: 

I would use the following line, to see that the response is actually the OK string, cause it may contain white space or other characters.

NSLog(@"string1 = '%@' temp = '%@'", string1, temp);

put this line before the if statement like this...

NSString *string1 = @"OK";
NSLog(@"string1 = '%@' temp = '%@'", string1, temp);
if([string1 isEqualToString:temp]){   // HERE IS THE PROBLEM, THE STRINGS ARE NOT GETTIN COMPARED

If extra whitespace is the reason why the comparison is failing then you can trim whitespace from you string by using the following method:

+(NSString *)trim:(NSString *)value {
    return [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
Panagiotis Korros
A: 

Another thing I would make sure is that you've received all the data before you check for the string. It looks like you do the string checking in didReceiveData, but it may make more sense to do the check in connectionDidFinishLoading after the data is complete.

danvin
A: 

Thank you Korros and danvin for helping me out. The problem was indeed with whitespaces. I trimmed the whitespaces as you said and it worked.

and Thanks stackoverflow.com . It is really a great forum.I find helpful ansers for all iPhone related topics, at the same time it also helps other platform as well i believe.