This is an example of how many developers have implemented receiving JSON responses from web services in Objective-C:

There are some problems with this approach. Some of the issues are specific to Objective-C, while others are more generally correctly using the HTTP protocol. Even if you are using a library/framework/cocoa pod that “does networking for me”, you should become familiar with these concepts.

First, this is performing networking on the main queue. That is a no-no, wether the networking is synchronous or not. Creating a new NSOperationQueue for your connections and passing that instead of [NSOperationQueue mainQueue] is a huge improvement.

Second, the error handling is incorrect. In general the correct error handling pattern for Objective-C is to check wether a call resulted in the expected result before using the error. In this case, it’s the NSURLResponse that should be checked, not the data. NSURLConnection may be able to connect to the remove service just fine but get no data back - and for many HTTP requests this is expected, correct behavior. If there is a problem connecting, the NSURLResponse will be nil . Check wether the response is nil, if it is then handle the error.

Third, the JSON parsing is incorrect. NSJSONSerialization will return an object of type id - there is no way to be sure that it will return an NSDictionary for a given input. Instead, it will return id , which can be almost anything. If NSJSONSerialization returns nil , it could not parse the data and the error parameter will be populated. The correct way to handle this would be to expect an id object back from NSJSONSerialization , check wether it is nil, and wether it responds to the selectors that will be used on it.

And then there are things that are not being done at all, which are necessary to handle the response correctly:

Checking of the MIME type

Checking the HTTP status code

Here is a more complete, and verbose example of doing this correctly. Obviously you will want to break this into smaller methods, but it should provide guidance for the logic that is required to handle JSON responses from web services correctly in Objective-C. While it uses NSURLConnection , the same lessons apply to NSURLSession .

Following the above steps will lead to a happier, more stable application.