If I want to hit REST services from an iOS app what is the most mature library for doing so? What are my best options?
Asked
Active
Viewed 1.0k times
5 Answers
7
You can use RestKit.
Or you can use NSJSONSerialization
which is already in the foundation framework.
Here's an exemple from a project that i made. It fetch an array of drinks from a json webservice:
NSString *urlString= [NSString stringWithFormat:@"my json webservice URL"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *jsonResultSet = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if ([jsonResultSet count] !=0){
NSArray* drinks = [jsonResultSet objectForKey:@"drinks"];
for (NSDictionary* drinkDictionary in drinks) {
Drink* drink = [[Drink alloc] initWithDictionary:drinkDictionary];
[[DrinkList getInstance]addDrinksWithObject:drink];
}
}

The iOSDev
- 5,237
- 7
- 41
- 78

Frank
- 790
- 5
- 10
1
I recommend AFNetworking. The following is a sample code taken from its Github page (there is more samples over there):
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];
Other alternatives are:
Of them all, I think both AFNetworking and RestKit are the most popular. I personally have used extensively AFNetworking, thus why I recommend it.

Jan S.
- 10,328
- 3
- 31
- 36
-
Have you used RestKit as well? – Aran Mulholland Oct 22 '12 at 06:38
-
I tested it for a project of mine a year ago, but haven't played much with it. If you are going to use Core Data, then RestKit provides easy integration with it. So it depends on your particular use case. – Jan S. Oct 24 '12 at 02:18
-
Both are mature libraries. I added other alternatives as well on my answer. – Jan S. Oct 24 '12 at 02:25
-
RESTKit built on top of AFNetworking. AFNetworking is a general Network toolbox. When refering to "RESTful" - what means there is the need for an RESTful abstraction layer - AFNetworking is inappropriate. RESTKit provides such a layer and even a powerful Client with memory, lifecycle and object management. – Rene Berlin Feb 20 '13 at 10:51
1
There is also the new swift rest library called Alamofire (https://github.com/Alamofire/Alamofire), it seems really nice, it has already more than 6000 stars on github and it has been created by Mattt Thompson which means you can go for it without worries.

mbritto
- 898
- 12
- 25