SVHTTPRequest is a simple and extremely straightforward way to communicate with RESTful web APIs for iOS and Mac. It’s a simpler and cleaner alternative to bulky ASIHTTPRequest, AFNetworking and RESTKit. It is blocked-based, uses NSURLConnection as well as NSJSONSerialization (if available, otherwise JSONKit – included in package) to automatically parse JSON responses.
SVHTTPRequest features:
- straightforward singleton convenience methods for making
GET,POST,PUT,DELETEand download requests. - completion block handler returning
response(NSObjectif JSON, otherwiseNSData) anderror. - persistent
basePathand basic authentication signing when usingSVHTTPClient. - support for
multipart/form-dataparameters in POST and PUT requests. - talks with the network activity indicator (iOS only).
- Drag the
SVHTTPRequest/SVHTTPRequestfolder into your project. #import "SVHTTPRequest.h"(this will importSVHTTPClientas well)
(see sample Xcode project in /Demo)
The easiest way to make a request is using the SVHTTPRequest convenience methods:
[SVHTTPRequest GET:@"http://github.com/api/v2/json/repos/show/samvermette/SVHTTPRequest"
parameters:nil
completion:^(id response, NSError *error) {
NSLog(@"SVHTTPRequest has %@ watchers", [[response valueForKey:@"repository"] valueForKey:@"watchers"]];
}];
If most of your requests are made to the same API endpoint, you should instead use SVHTTPClient so you can set parameters (basePath, cachePolicy, sendParametersAsJSON, "userAgent) that will be used for each request:
[[SVHTTPClient sharedClient] setBasePath:@"http://api.twitter.com/1/"];
[[SVHTTPClient sharedClient] GET:@"users/show.json"
parameters:[NSDictionary dictionaryWithObject:@"samvermette" forKey:@"screen_name"]
completion:^(id response, NSError *error) {
followersLabel.text = [NSString stringWithFormat:@"@samvermette has %@ followers", [response valueForKey:@"followers_count"]];
}];
If you would like to set those properties on individual requests, you’ll need to alloc/init the request, set the attributes, and then call start:
SVHTTPRequest *request = [[SVHTTPRequest alloc] initWithAddress:@"http://github.com/api/v2/json/repos/show/samvermette/SVHTTPRequest"
method:SVHTTPRequestMethodGET
parameters:nil
completion:^(id response, NSError *error) {
watchersLabel.text = [NSString stringWithFormat:@"SVHTTPRequest has %@ watchers", [[response valueForKey:@"repository"] valueForKey:@"watchers"]];
}];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[request start];
You can tell SVHTTPRequest to save a GET response directly to disk and track the progress along the way:
[SVHTTPRequest GET:@"http://example.com/db.sqlite.zip"
parameters:nil
saveToPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"store.zip"]
progress:^(float progress) {
progressLabel.text = [NSString stringWithFormat:@"Downloading (%.0f%%)", progress*100];
}
completion:^(id response, NSError *error) {
progressLabel.text = @"Download complete";
// process file
}];
Make sure you cancel requests for which the user isn’t waiting on anymore:
SVHTTPRequest *request = [SVHTTPRequest GET:@"http://api.twitter.com/1/users/show.json"
parameters:[NSDictionary dictionaryWithObject:@"samvermette" forKey:@"screen_name"]
completion:^(NSObject *response) {
NSLog(@"%@", response);
}];
[request cancel];
If you’re using SVHTTPClient, you can do that by calling cancelRequestsWithPath: or cancelAllRequests.
All SVHTTPRequest requests are made asynchronously using NSURLConnection’s built-in asynchronous methods. The completion block, however, is executed on the main thread. You should dispatch it to a separate thread/queue if it’s resource intensive enough that it hogs the main thread. This can be done easily using Grand Central Dispatch:
completion:^(NSObject *response) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// cpu-intensive code
});
}];
Maintaining an official ARC branch has proven to be too much work, often leading to confusion since the ARC branch is always a few commits behind. If you’d like to use SVHTTPRequest in your ARC-enabled project, you’ll have to add the -fno-objc-arc compiler flag to all of SVHTTPRequest’s files.
SVHTTPRequest is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you’re using SVHTTPRequest in your project, attribution would be nice.