Reputation: 685
I have searched google for the different method and libraries used to consume a RESTful web service. But most of them are either outdated or failed to work when I tried the old libraries. This could be due to the fact that some of there were discontinued and not compatible with the new SDK.
I have tried SBJSON, ASIHTTP, stig's and jsonframework, but non of them seem to be working.
What are some of the current libraries that are being used in iOS to consume RESTful webservice? It will be helpful if anyone can give link to sample tutorial using the same libraries.
Upvotes: 2
Views: 4857
Reputation: 12839
Use the built-in NSURLSession
and NSJSONSerialization
and then use Github's Mantle framework to map the JSON dictionary to your custom ObjC objects.
Upvotes: 0
Reputation: 19926
Try RestKit: http://restkit.org/
It is well known, popular, consumes XML as well as JSON and works with Core Data (response to NSManagedObjects mapping) which make great backend as a local cache.
Upvotes: 6
Reputation: 224
Why dont you use iOS API classes like NSURLConnection? (iOS5 or above required I think)
You could invoke REST GET opperation for example like this:
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:GET];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];//for https
connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
Where url should be an NSURL object pointing to your rest service operation url. And declare the corresponding delegate methods:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
code = [httpResponse statusCode];
NSLog(@"%@ %i",@"Response Status Code: ",code);
[data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:nil
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
self.connection = nil;
self.data = nil;
}
connection, data and code could be local variables to your implementation class. In those variables you are going to store the connection made, the JSON data received (or whatever) and the response http code like 200, 404.
And finally if you are planning to invoke a secured REST service, dont forget to include the authenticationchallenge delegate.
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//set the user and password loged in
NSString *username = @"username";
NSString *password = @"password";
NSURLCredential *credential = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
Hope this helps!
Upvotes: 3