Reputation: 17185
I have some code that looks like it should work which tries to send an Asynchronous request, but the request is never being generated:
Here is the function:
-(void) setEmail: (NSString *) subject andBody: (NSString *) body
{
NSString *urlString = @"http://www.my_url.com?";
// Create encoded query string
NSString *query_string = @"subject=%@&body=%@";
NSString *query_with_args = [NSString stringWithFormat:query_string , subject , body];
// Now encode the query
// NSString *encodedQuery = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
// (__bridge CFStringRef)query_with_args,
// NULL,
// (CFStringRef)@"!*'();:@&=+$,/?%#[]",
// kCFStringEncodingUTF8);
//
// Now concatinate url and query
NSString *final_url = [urlString stringByAppendingString:query_with_args];
NSURL *url = [NSURL URLWithString:final_url];
// Now send to the server
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// TODO: ok I dont really understand what this is
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSLog(@"......before request");
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"On return");
NSLog(@"This is data: %@" , data);
NSLog(@"This is response: %@" , response);
NSLog(@"This is error: %@" , error);
NSLog(@"OK");
}];
}
Any idea what might be going wrong here and why the request is never sent? The logger with the
NSLog(@"......before request"); does get printed.
Upvotes: 1
Views: 1324
Reputation: 8501
Try like this.. You need to implement NSURLConnectionDelegate
Protocol and methods in it.
NSString *final_url = [NSString stringWithFormat:@"http://www.my_url.com?subject=%@&body=%@",subject, body];
NSURL *url = [NSURL URLWithString:final_url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
Hopefully This one will help you.. http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
Upvotes: 1