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.
Related
I'm not sure this question is appropriate here but I hope I could get some help. What i'm looking to do is basically to make a server based iPhone application. I've got an ubuntu server ready to be used. I'm thinking of making a web service which my application then can use but I have virtually no experience in this field so i'm looking for some help to get pointed into the right direction, what language to write the web service in etc. It isn't required to be a web service but anything that make's the iPhone app depending on the server.
This little mission is simply for learning experience.
Probably PHP is what you will use to write the webservice. REST based services are the most popular, but it could be as simple as a form POST passing in parameters. The PHP script (no matter which way you write it), will then pass those parameters and any other needed info to a server method that either executes a SQL query (preferably using a stored procedure) or uses some other server resource. That data would then be output via the webservice as either XML or JSON (I personally like JSON better).
Check out Ray Wenderlich's tutorial on just this kind of thing:
When it comes time to handle the request in iOS, my personal choice is AFNetworking. It uses blocks instead of delegate methods and is blazing fast. I can hook you up with examples if needed.
Ok, First you need to learn how to write web services in PHP. Which will return you response in XML format. I personally prefer .PLIST format because it gets easier to parse on the iOS.
Once your web services are up and running. You need to use NSURLConnection and NSURLRequest to talk to your web services. Implementing the NSURLConnection Delegate methods correctly, you can download your XML response, maybe save it in a file on your Documents Directory.
Then once done downloading start parsing that response using NSXMLParser. implementing the delegates and what not, you can create your data structure the way you feel comfortable with manipulating it and store the parsed results in them and populate your View accordingly.
I am bit confused regarding SOAP and REST and JSON
I do not know what step and what is the right way to implement this in objective-C
I want to send json data to a web-service via SOAP.
I should use RESTful or SOAP.
Any document or link would be appreciated.
Let me give you some advise out of personal experience regarding this.
A few things first, if you are in control of the web service I would recommend going for RESTful and returning and sending JSON data as parsing it using Objective-C is very easy.
There is a class library called SBJson you can use for the parsing of JSON data returned by the web service.
If you are not in control of what the web service will be returning and receiving, i.e. you didn't program the web service you can still use the SBJson classes for JSON data but I would recommend finding a XML library which you are comfortable in using.
http://www.raywenderlich.com is a great source of iOS tutorials with a in depth tutorial on XML parsing, which is used with SOAP.
The SBJSson library can be found at https://github.com/stig/json-framework/
Does these help?
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.
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.
We have an iPhone application created by an external consultancy that we're planning to add card payment facilities to in a subsequent release.
We plan to host a service ourselves in order to process the payment stuff, with SSL encryption. We have in-house expertese for all of this apart from the (contracted out) iPhone bit.
Are there any specific gotchas that we should be aware of that concern designing web services for iPhones?
We'll be writing the web service in C# 3.5.
JSON data format is better to be converted into NSArray or NSDictionary objects. It's easier and faster to be parsed.
So, specifically for the iPhone, it's a lot better to consume JSON data. Unless if there's some technical complexity that JSON is unable to handle.
Check YAJL:
http://github.com/lloyd/yajl
There are Objective-C wrapper/implementations by gabriel in github and by MGTwitterEngine.
TouchJSON is another code that's simpler than yajl. You can convert JSON string into NSDictionary or NSArray object in 2 lines of code. But, it maybe slower.
I'm not sure there are really any special considerations. The iPhone should be able to communicate with most types of webservice.
I worked on an iPhone app that communicated to a RESTful webservice written in Java.
I imagine it's pretty straightforward across the board - there are plenty of libraries for parsing/generating XML or JSON formatted messages, the iPhone can handle HTTP authentication, HTTPS, caching, etc.
It's just down to your iPhone developer to get it right :)
For SOAP based web services I strongly suggest that you try gSOAP. This library does not support Objective-C, however it supports C and C++ and is certainly the most complete open source project to access SOAP based web service; it also outperforms all of the other libraries.
For Objective-C you may want to try wsdl2objc, but I am not sure if it provides support for SSL/TLS (gSOAP does).
Finally, REST based web services are easily handled using ASIHTTPRequest.