0

All,

This might seems very straightforward but I have already gone through the URL loading system program guide and the various NSURL.... classes and not found my answer.

I am just trying to do a simple call from the app to a website to reserve a party spot:

NSString *path=[NSString stringWithFormat:@"http://www.myparty123.com/ireservedURL.asp?Nickname=%@&Seat=%i&Email=%@&Currentparty=%i",john,5432,john@yahoo.com,6598];
NSURL* reservationURL =[NSURL URLWithString:path];
NSData *call= [NSData dataWithContentsOfURL:reservationURL];   

This seemed to be the simple way but I get this warning that I am not using the "call" variable. And it seems overkill to do a call using NSData where there is no data return.

Yet, creating an NSURLRequest and initiating an NSURLConnection and then implementing delegates with that request seems to be overkill as well.

What am I missing.

Thanks KMB

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62

2 Answers2

5

NSURLConnection has a method called sendSynchronousRequest:returningResponse:error: that allows you to download NSData from a URL without implementing the NSURLConnection delegate.

So your code could be rewritten:

NSString *path = [NSString stringWithFormat:@"http://www.myparty123.com/ireservedURL.asp?Nickname=%@&Seat=%i&Email=%@&Currentparty=%i",@"john",5432,@"john@yahoo.com",6598];
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

You'll no longer get the warning because you aren't assigning the data to a useless variable. If you want to know if the response was a success you could create a NSURLResponse variable and pass it into the sendSynchronousRequest method call.

Something to note is that this method will block whatever thread you call it on. So if you call this on the UI thread and the download takes 10 seconds the user will be unable to interact with the app. Implementing the NSURLConnection delegate or using the sendAsynchronousRequest:queue:completionHandler: method will not block the thread they are used on.

You could also wrap this code inside a NSInvocationOperation and add it to a NSOperationQueue or look into something like AFNetworking.

Reid Main
  • 3,394
  • 3
  • 25
  • 42
  • He can just send the asychronous request without assigning to result to a variable. You did fixed the warning by adding a log which is not a desired solution. And suggesting AFNetworning, a big library, for such a simple task? – Sulthan Sep 12 '12 at 15:40
  • Good call Sulthan. I've update the answer. I suggested AFNetworking just so if they have to do anything more complex in the future they have an idea of the libraries out there before they start creating their own. – Reid Main Sep 12 '12 at 15:42
  • Thanks to both. This gets me now thinking that I could have done the same thing for NSData as [NSData dataWithContentsOfURL:reservationURL]; without declaring "call" var and this would avoid an unused var warning. I am guessing the only 2 advantages of [NSURLConnection sendAsynchronousRequest:request queue:OtherQueue completionHandler:nil]; is that the return of this method is truly a void and the built in multithreading. Correct? – Khaled Barazi Sep 12 '12 at 21:01
  • Basically. I wouldn't be surprised if [NSData dataWithContentsOfURL:reservationURL] also used NSURLConnection behind the scenes but you always want to be doing any communicating with a web server off the UI thread. – Reid Main Sep 13 '12 at 14:16
0

The simplest way to send a request as far as know is the + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)errormethod of NSString

Jakob W
  • 3,347
  • 19
  • 27