So I'm brand new to rest's API and have never used an API ever before. I'm ok with Objective-C and Cocoa Touch but just have no clue where to start when accessing the API and how to in general. Can someone help me get started with some code that will access titles in rest or just how to access a REST API in general with authentication. Thanks.
Try http://allseeing-i.com/ASIHTTPRequest/
Good framework!
Take a look at RestKit: http://restkit.org/
It features a high level object mapping framework that can turn remote RESTful responses back into local objects declaratively, handling all the parsing and mapping for you.
http://github.com/mirek/NSMutableDictionary-REST.framework is easy to use.
To get REST as dictionary from GitHub API as an example:
NSString *url = #"http://github.com/api/v2/xml/user/search/mirek";
[NSMutableDictionary dictionaryWithRESTContentsOfURL: url];
To post:
[myDict postRESTWithURL: #"http://localhost:3000/my-rest"];
It supports sync/async, intuitive array conversions, url encoded and multipart posts (you can send images and other files).
Related
For my project course I'm thinking to develop an application in iPhone. I have some objective-c knowledge but I don't know what kind of API's Apple provide.
I've implemented RESTful web service and I was wondering if there is a way to access features of this RESTful API via iPhone. I mean, what kind of API's area available to send HTTP GET, POST, ... and to process JSON/XML data returned by web service?
Any help is very much appreciated.
All HTTP request methods (POST, GET, PUT, DELETE, etc…) are available with NSURLConnection.
You can parse XML with NSXMLParser and you can parse JSON with JSON-Framework (it's really good).
This tutorial may be helpful: Tutorial: Simple iPhone Rest Client
A great and widely accepted API for HTTP requests/etc is ASIHTTPRequest, here is the link. As far as XML parsing, you might want to take at the Apple provided NSXMLParser (note there could be external APIs for this, but I am unaware of them). Here is its class Reference
I am developing application in iPhone, Where i need to download and upload data from webservice. Please advice me which is the best class or Framework to achieve this and also please let me know which is the best xmlparser.
you can use asihttprequest and asihttpresponse for establishing connection use restful api and nxxml parser for parsing the data comes from server
thanks
NSXMLParser is a SAX parser included by default with the iPhone SDK.
TouchXML is an NSXML style DOM XML parser for the iPhone.
If your web service has the ability to return json, I would highly recommend doing so and using http://code.google.com/p/json-framework/. Especially if you have a complex XML document.
As for connecting to the service, it's pretty straightforward - all you need is a method to handle the connections. While there are 'frameworks' out there, it's as simple as creating a method such as "getDataFromService:(nsstring *)param1" and within that method issuing a request.
I think the bigger question you'll need to answer is how to authenticate with the service, if required. If the service uses OAuth, http://code.google.com/p/gtm-oauth/ is a great framework.
I have a general question. Using the NSMutableURLRequest object in the iPhone SDK, can I set the HTTPMethod to POST when retrieving data from a server?
Normally you should either POST some data or GET some data, though issuing a POST and then fetching some data in a single request is perfectly valid.
Just to be clear, it is in fact quite common for a POST to return some data representing some sort of result message in response to the POST. Other uses (like piggy-backing an unrelated GET onto the POST) would be frowned upon.
If you are wanting to be HTTP / web standards compliant, you should use a GET when retrieving data. It will also depend on if the application you are talking to on the server will respond to a POST request for the given resource.
If you're willing to use third party libraries I can highly recommend ASIHTTPRequest:
http://allseeing-i.com/ASIHTTPRequest/
I've used this in library for http posts in a number of iPhone applications and been very happy with it.
Just to clarify: I'm not associated with these guys in any way, just like the library!
been wrestling with this for some time. I am trying to access a REST api on my iphone and came across the ASIHTTP framework that would assist me. So i did something like
//call sites, so we can confirm username and password and site/sites
NSURL *url = [NSURL URLWithString: urlbase];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUsername:#"doronkatz%40xx.com" ];
[request setPassword:#"xxx"];
Where urlbase is a url to a REST site.
Now, a developer has told me there might be an issue or bug with this framework, and its not passing headers correctly. Is there another way of testing or accessing with authentication a network REST location?
I would recommend checking out RestKit: http://restkit.org/ It provides an excellent API for accessing RESTful web services and representing the remote resources as local objects, including persisting them to Core Data. HTTP authentication is supported out of the box.
I'm new to iOS development and I've been battling with some of the big frameworks listed on this page for the past month or so. It has been a nightmare. I'd honestly recommend you just stick to the basics and do it yourself using AFNetworking or Apple's own NSURLConnection.
One of the libraries listed is no longer maintained. Another underwent huge code-breaking API changes recently and now half of the tutorials describing its use no longer work. Others are massively bloated.
It's easier than you'd think. Some resources that helped me:
http://blog.strikeiron.com/bid/63338/Integrate-a-REST-API-into-an-iPhone-App-in-less-than-15-minutes
http://www.slideshare.net/gillygize/connecting-to-a-rest-api-in-ios
The examples on the AFNetworking homepage alone may get you 80% of the way there.
UPDATE: The Mantle Framework (open sourced by Github Inc.) is well-designed and easy to use. It handles object mapping: converting a JSON NSDictionary to your own custom Objective-C model classes. It handles default cases sensibly and it's pretty easy to roll your own value transformers, e.g. from string to NSURL or string to your custom enum.
I have a couple of apps using a framework called Objective Resource which provides a wrapper for accessing remote REST based api's. It is aimed primarily at Ruby on Rails based applications so it's XML/JSON parsing may be tuned to handle some Rails defaults but it is worth looking at. It supports http basic authentication by default.
Just stumbled on this question - you might find LRResty pretty interesting as it uses NSOperation, blocks etc., see: GitHub for source (MIT license). I'm experimenting with it now - it has a sample app too.
I've used ASIHTTP in two apps so far and have had no problems.
Looks like you're doing HTTP Basic Auth with the remote site. Try hitting the same REST URL from a standard browser and pass the params you need down to it. It should prompt you for username/password. If it makes it through, then at least you know the server-side is set up to handle requests. If it doesn't, then you need to have a talk with the dev.
The next thing to try is put a Mac-based network sniffer and see what headers are going back and forth. Any of HTTPScoop, Wireshark, or Charles should work. Run the sniffer as a network proxy then run your app in the simulator and watch what goes across. Try it again with the browser. Once you see the differences, you can use the addRequestHeader method on ASIHTTPRequest to add any specific headers the server expects.
I know how to connect to web server using an iPhone but now I have to connect the iPhone to a web service. I don't know how to do it and there is no demo or class available online.
Does anyone have any ideas?
You might find this tutorial, called Intro to SOAP Web Services useful. He shows how to package a request, send it to a web service, and read the response.
If you need some help with XML parsing, there is the TouchXML library which will give you a nice xml "document" to work with. Just be cautious of memory usage.
If you have to parse large XML message this tutorial about libxml and xmlreader in Cocoa will show you how to parse XML with the lower-level event-style parsers.
I've created an open source application for iPhone OS 3.0 that shows how to use REST & SOAP services in iPhone application, using XML (using 8 different iPhone libraries), SOAP, JSON (using SBJSON and TouchJSON), YAML, Protocol Buffers (Google serialization format) and even CSV from a PHP sample app (included in the project).
http://github.com/akosma/iPhoneWebServicesClient
The project is modular enough to support many other formats and libraries in the future.
The following presentation in SlideShare shows my findings in terms of performance, ease of implementation and payload characteristics:
http://www.slideshare.net/akosma/web-services-3439269
You can use these 2 lines which return the response of your HTTP request. You don't need any configuration. This code is usefull if you try to access a PHP scritp for example. After you just have to parse your result.
NSURL *URL=[[NSURL alloc] initWithString:stringForURL];
NSString *results = [[NSString alloc] initWithContentsOfURL :URL];
In my opinion, you have two options :
Use a third party library. You can try wsdl2objc. It didn't work for me, but it is under active development so it improves every day.
Use a raw HTTP connection and handle every request/response. This is the way I followed. It is hard, so I'd also like to know a better approach.