I’m a novice in iOS developing, and have some problems with understanding web service organization. I want to send a Get query to the URL. And I do it so:
-(BOOL) sendLoginRequest{
NSString *getAction = [NSString stringWithFormat:@"action=%@&username=%password=%@",@"login",self.log.text, self.pass.text];
NSString *getUserName = [NSString stringWithFormat:@"username=%@",self.log.text];
NSString *getPassword = [NSString stringWithFormat:@"username=%@",self.pass.text];
NSData *getDataAction = [getAction dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLengthAction = [NSString stringWithFormat:@"%d", [getDataAction length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http:http://www.site.fi/api/"]];
[request setHTTPMethod:@"GET"];
[request setValue:getLengthAction forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getLengthAction];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSAssert(self.urlConnection != nil, @"Failure to create URL connection.");
// show in the status bar that network activity is starting
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
the answer may be “true” or “false”
but how can I take this answer?
You should define next methods to get answer:
Start connection:
[self.urlConnection start];Check server response:
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)responseCollect data that servers sends you:
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)dataBe sure to manage errors:
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)errorCheck received data:
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnectionTo be more sure that you correctly understood me check NSURLConnection Class Reference