Installing

Download the Objective-C Unirest Library from GitHub (or clone the repo) and import the folder into your project. You can also install Unirest-obj-c with CocoaPods.

CocoaPods

If you decide to use CocoaPods, create a Podfile file in your project’s folder:

$ edit Podfile
platform :ios, '5.0'
pod 'Unirest', '~> 1.1.3'

and then execute pod install. Make sure to always open the Xcode workspace instead of the project file when building your project:

$ open App.xcworkspace

Now you can import your dependencies:

#import <UNIRest.h>

Requirements

The Unirest-Obj-C client library requires ARC (Automatic Reference Counting) to be enabled in your Xcode project. To enable ARC select your project or target and then go to Build Settings and under the section Apple LLVM compiler 3.0 – Language you will see the option Objective-C Automatic Reference Counting:

For existing projects, fortunately Xcode offers a tool to convert existing code to ARC, which is available at Edit -> Refactor -> Convert to Objective-C ARC

Creating Request

So you’re probably wondering how using Unirest makes creating requests in Objective-C easier, let’s look at a working example:

Just like in the Unirest Java library the Objective-C library supports multiple response types given as the last parameter. In the example above we use asJson to get a JSON response, likewise there are asBinary and asString for responses of other nature such as file data and hypermedia responses.

Asynchronous Requests

For non-blocking requests you will want to make an asychronous request to keep your application going while data is fetched or updated in the background, doing so with unirest is extremely easy with barely any code change from the previous example: