We all loved the magnificent networking framework by Ben Copsey, but in a heartfelt post he said it doesn’t make sense for him to continue updating it and converting it to ARC.
So … ASIHTTPRequest is dead now what?
We will have to find another networking framework to work with.
I for one have been using MKNetworkKit and even if it is not as mature as ASIHTTPRequest is right now, I’m betting on this framework because:
- I know Mugunth’s work from times going back to iOS 3.x and I know he writes code that works
- he’s putting a lot of effort and adds features regularly
- it is ARC compatible (and not compatible like cocos2d is, but really compatible) and this is something I really look for into a library these days
- it’s blocks based
- handles server ETag markers, which means you can control how the app caches HTTP requests on the server side (this means for example, you can at once flush the cache on all devices from your server)
- handles “operation freezing” – requests can pile up until the app is reconnected to the Internet
- much more …
Let me show some examples on how to use it.
Getting JSON from a web service
//init the http engine, supply the web host //and also a dictionary with http headers you want to send MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"api.kivaws.org" customHeaderFields:nil]; //request parameters //these would be your GET or POST variables NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"fundraising",@"status",nil]; //create operation with the host relative path, the params //also method (GET,POST,HEAD,etc) and whether you want SSL or not MKNetworkOperation* op = [engine operationWithPath:@"v1/loans/search.json" params: params httpMethod:@"GET" ssl:NO]; //set completion and error blocks [op onCompletion:^(MKNetworkOperation *completedOperation) { NSLog(@"json: %@", [op responseJSON]); } onError:^(NSError *error) { NSLog(@"%@", error); }]; //add to the http queue and the request is sent [engine enqueueOperation: op]; |
So things are pretty straight forward. You can reuse the engine to make many requests to the same web hosts, and just ask it to produce different operations. I like very much how easy it is to create requests with different methods – basically passing variables via GET or POST is exactly the same, you just need to change the text in the httpMethod param. On the other hand there’s something I really hate about the engine-operation implementation and it is that you cannot change the custom headers you want to be sent per request. For some reason the custom headers are stored in a private property so there’s no way to change them after you create an instance of the engine.
You want SSL? Just pass YES for the ssl param. Piece of cake.
Also you probably noticed the handy responseJSON method on the completed operation which gives you back directly the JSON object returned. Pretty cool.
Using this code you can pretty much get anything back from a web server – HTML, JSON, binary data (as images, download files and such) Just have a look at the different methods on the completed operation:
- responseData
- responseString
- responseJSON (only iOS5 via NSJSONSerialization)
- responseImage and
- responseXML.
You are now mastering MKNetworkingKit
However there’s one handy method I really want to mention. Let’s see how can you download images:
//set the image's URL NSURL* url = [NSURL URLWithString:@"http://mycharitywater.org/images/instruct_graphic.jpg"]; //download the image [engine imageAtURL:url onCompletion:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) { UIImageWriteToSavedPhotosAlbum(fetchedImage, nil, nil, nil); }]; |
So, this is all the code you need to asynchronously download an image from the web and save it to the user’s device. Pretty cool eh?
Installation and usage
- Download from github: https://github.com/MugunthKumar/MKNetworkKit/tree/master/MKNetworkKit
- Open up Build Phases for your current target and add the following libraries: CFNetwork, SystemConfiguration and Security
- To have appwide access to MKNetworkKit, find the pch file in your project files and after the last import add:
#import "MKNetworkKit.h" - If you’re building for iOS: delete NSAlert+MKNetworkKitAdditions.h/m, for OSX delete UIAlertView+MKNetworkKitAdditions.h – those files are located within the MKNetworkKit/Categories (this could’ve been done more elegant right?)
You’re ready to go!
The post was originally published on the following URL: http://www.touch-code-magazine.com/asihttprequest-is-dead-now-what/
·



What do you think of RestKit?(http://restkit.org/)
Hey, didn’t have chance to look at it, and there’s still at least 2 more good networking libraries out there … but I don’t really have the time to test them all and go through the code, etc. as I’d like to in order to make my mind which on is the best to use
You can just turn off ARC for the ASI .m files and it works perfectly on iOS 5.1 and 6.0 so im not dropping this easy
hi Marc, that’s definitely alright to do, the problem is the announcement that it won’t be developed further … it’s a pity, and it is an awesome lib, but there are several newer, more modern ones – I still think it’s better to go on
Marin
Thank you for pointing to this library. Charmingly simple yet powerful.
Thank you for pointing to this library. Charmingly simple yet powerful.