Since that webservice is a hot topik on the forum, I decided to write a guideline to understand what you can do when you need to use webservice from iOS (so iphone or ipad or ipod, etc...)
INDEX:
Section 1: About Webservice
Section 2: REST webservice
Section 3: SOAP Webservice
Section 4: HTTP Request
Section 5: Parse XML
Section 6: Parse JSONIn the first Section we will see some basic things about webservice, in Section 2 and 3 we will see 2 important architectures of webservice and how we can use these. In section 4 we will see some example to communicate with webservice, and in section 5 and 6 we will see how we can use what the webservice return.
Section 1: About Webservice
Basically is a server that expose methods, you can call these methods with an input and you will receive an output. A webservice can be written in any language (php, C#, VB.Net, Java, Python, etc...).
Really webservices are much more than that, but this is not the place to study the argument in dept.
In iOS SDK there isn't a framework for consume webservice, so you need to create something from scratch, this can be "easily" done because webservice generally support HTTP + XML or JSON (in some case HTTP is not enabled for default, you need to enable manually yourself).
WSDL is a file that describe in depth the webservice (so contain a list of methods, with detail for their input and output, more other stuff)
XML Is a standard way to represent data in textual format.
JSON is another standard way to represent data in textual format.
SOAP and REST are 2 architectures of webservice. The first use SOAP messages (subset of XML) to communicate with webservice, so you will use http requests to send\receive SOAP messages to communicate with the webservice. With REST you can use http requests using GET (or POST, PUT, DELETE) to call a method of webservice, and XML or JSON as input and output.
There are many other things to know about webservices, start reading from the link posted above if you want have a good explanation of the topik.
Usefull links (tutorials + other stuff):
http://www.iphonedevsdk.com/forum/iphone-sdk-development/2841-resolved-how-call-soap-service.htmliPhone SDK: First Steps With JSON Data Using the Twitter APITutorial: JSON Over HTTP On The iPhone | Mobile OrchardJSON Framework for iPhone (Part 2)iPhone Programming Tutorial ? Intro to SOAP Web Services | iPhone Programming Tutorialshttp://github.com/akosma/iPhoneWebServicesClientConsuming XML Web Services in iPhone Applicationshttp://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-serviceNOTE1: This tutorial is not intended to be a perfect explanation of webservice (so some term and phrase can be not 100% exact), it is just a guideline to consume webservice with iOS SDK.
NOTE2: My english is not so good, so if you find some error, tell me privately, i will fix it.
NOTE3: Feel free to add some usefull informations (links, approaches, utility, etc...), I will try to keep updated the thread with all options possible.
Replies
REST is the easier to consume, it generally have a link, where you can specify a method and parameter directly with GET (GET mean inserting parameters directly on the link) Example: http://www.server.net/rest/?method=myMethod&par1=ok
So what you need to use a REST webservice is just to know the link with the right method and right input for that and then you will call it using an http request. [Look Section 4: HTTP Request]
You can decide also to use some specifics libraries to consume REST WebService
HTTPRiot HTTPRiot: HTTPRiot - A simple HTTP REST Library
ObjectiveResource iPhone on Rails and ObjectiveResource; Making communication between the iPhone and a Rails web-service pain-free.
These have the aim to make even easier consume a REST webservice, HTTPRiot support JSON, ObjectiveResource support XML and JSON, and both came with some samples to start using it.
Section 3: SOAP webservice
I will show you 2 way to understand how you can create right SOAP Messages.
1) Software that generate code:
SudzC is one of these,
Link: SudzC (alpha) | clean source code from your web services
It is a great tool that generate objective-c code ready to use, starting from WSDL (WSDL is a complete description of webservice, so in it you can read input\ouput of all webservice methods)
Recently was not updated often but the developer said that he will update soon his project adding new features and fixing bugs.
it can work, or can produce something that not fully work. Try it and see if you have some problems. In this case, you can try search on the forum to find some thread about sudzc.
Another software is wsdl2objc, but avoid it, is too pre-alpha and moreover outdated.
Link: wsdl2objc - Generates Objective-C (Cocoa) code from a WSDL for calling SOAP services - Google Project Hosting
2) Create "manually" xml to send with HTTP request and parse XML that you receive
With this solution, you should create the classes that represent the objects that your method(s) need (you can also start from the classes generated by sudzc). and then you will create a http request to your webservice, it will reply with an xml to parse.
What you want try to do is call some method of your webservice, so you need to understand what webservice expect to have when you call that method and what the webservice will return as output (so that you can parse it). You can read this information looking at the WSDL, but sometime when webservice is not easy\little, can be hard to fully understand what you need directly from WSDL.
So, you can use soapUI, it can help you a lot.
1)download soapUI
2)file\new soap project, and insert link to your wsdl
3)expand method that you want launch, double click "Request", you will see the xml that you should produce to call method.
4)recreate it manually on your iphone (concatenating strings and variables)
5)Send in post to your webservice link asynchronously (you can use ASIHTTPRequest or NSURLConnection). [Look Section 4: HTTP Request]
6)The server will reply with another xml that you can parse. [Look Section 5: Parse XML]
Another way to know what you need to send in xml, is too use a sniffer (wireshark? fiddler? tcpmon?) and see the xml that your "standard client" send\receive (assuming that you have a working client), and then use it as described in points 4,5,6.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHow you can do a HTTP request? Easy, using NSURLConnection or ASIHTTPRequest.
For REST webservice generally you need just to do a request to the link (generally using just GET to specify input).
For SOAP webservice generally you need to do a request to the link of your webservice and add your SOAP Message in POST (that contain the method to call and the input for that method)
You would do the request asynchronously, seen that you want to do the request in background without block User Interface, so now we will see how to do asynchronous http requests with NSURLConnection and with ASIHTTPRequest.
NOTE:
NSURLConnection is built in with iOS SDK so you don't need to include nothing to use it, ASIHTTPRequest is a third party library that allow you to easily use http request and more stuff related. In order to use it in your project you must follow this ASIHTTPRequest Setup Instructions
Using ASIHTTPRequest for WebService (when you don't need to specify an input in POST)
How you can know the name of the methods and their parameters?
Check if your webservice have a WSDL or WADL, or check the documentation (generally who offer a webservice create also a documentation)
Using ASIHTTPRequest for Webservice (when you need to specify an input using POST)
What you need to insert in yourPOSTstring? A SOAP Message o a JSON string that contain your input (depend on what expect your webservice)
Now that you are able to call your webservice and retrieve its response, you just need to parse it, [Look Section 5: Parse XML] or [Look Section 6: Parse JSON]
NOTES:
Your REST WebService need to use PUT?
Your REST WebService need to use DELETE?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYourViewController.h
YourViewController.m
Then in connectionDidFinishLoading, we have responseString, the output of our webservice.
Using NSURLConnection (async) WebService (when you need to specify an input using POST)
The code is really similar, we just need some changes to add POST.
YourViewController.h
YourViewController.m
Then in connectionDidFinishLoading, we have responseString, the output of our webservice, you just need to parse it, [Look Section 5: Parse XML] or [Look Section 6: Parse JSON]
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNow that we have a response from the server, generally we want to parse it to create an object of the class that represent the object(s) returned.
We will see how to do that with an example.
Suppose that we have a webservice method getUser(id) that return the information about a user giving an id.
So the responseString from webservice when we call that method will be something like this:
Nice, so we have the following class that represent it
WS_User.h
let me show the code to parse it
ParserUser.h
ParserUser.m
This example cover various situations (when you need to parse an int\boolean, or an array of value (like friends, an array of strings) or a value that is on an attribute (like username)
Nice, so now we should just call this
We should have our user in myUser, now starting from that you should be able to make a parser for your objects.
NOTE:
When a WebService return an error, return a particular object: SoapFault, so you should create a SoapFault object and a SoapFaultParser that parse your responseString searching for a SoapFault, and when you are sure that responseString is not a fault, you should call ParserUser to parse the string as a WS_User.
I haven't done it in this tutorial just to keep it simple, you can see some code to do it from sudzc generated code, or you can just create it starting from my example.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIn the same manner of xml section, we will se an example for JSON parsing.
But first we need to see how to setup project to use it, because there is any JSON framework included with iOS SDK,
1) you need to download here SBJson
2) add all the files on folder "Classes" with Drag&Drop on your project, (select the "Copy items into destination group’s folder" option)
Now you should only import SBJson.h in your class.
Suppose that we have a webservice method getUser(id) that return the information about a user giving an id.
So the responseString from webservice when we call that method will be something like this:
Now the code to parse it.
Explanation:
responseString contain our json output returned by server, to parse it, we just need to call the method JSONValue, this return a NSDictionary with all the values.
dictionary is a NSDictionary, in our case it have 1 element, that element is in "return" key and is another NSDictionary. We assigned that to dictionaryReturn.
dictionaryReturn contain 5 objects (4 string: username, password_md5, banned, numPost and 1 NSArray). The array contain 3 NSDictionary each with 1 String on it (with key friendName), so we need to loop on the array, take the NSDictionary and then take the NSString from that.
NOTE:
Since that our webservice return only info about 1 user, we have a NSDictionary returned, if webservice return more than a user, it would returned a NSArray, so we would used
and so we would looped on the array that contain X NSDictionary, each representing a single user.
Finally you can fill your classes that represent the user (WS_User), or you can use directly what JSON Framework returned, as you prefer!.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeEnjoy.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeMy idea is that iOS creates a HTTP request which will include some kind of parameter like user name and password. Then my index.php page will check this name + pass whether user is allow to dig data and if so, index.php will make him JSON. Is that good way or is there something better???
THX
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks again!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe xml to send to webservice (soap message that contain envelop, etc...), can be for example on a file, so that you can retrieve it, and add information on it.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThank you so much...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI have a website with java as the back end and jsp as the front end , and mysql as the database. I am trying to follow youe tutorial which looks great but can anyone tell me can this tutorial be used if I do not use SOAP or REST.
Can I make the resquest the same way to the http to my jsp page that accesses the jsp and intefaces with the database adn send out a request?
thanks in advance.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIf you already have a SOAP setup, then use SOAP; otherwise, RESTful http is much easier to go. Make sure you return a JSON data set (or XML) and the "Content-Type" header in your return is "application/json".
Also, make sure you put proper authentication on who can call request which data.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeits cover all,from ABC of webservice...
its very useful to me...thanx :)
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThere is one thing I'm struggling with though ASIHTTPRequest and JSON...
If I use...
NSString *pass = (NSString*) [dictionaryReturn objectForKey:@"password_md5"];
NSLog(@"password: %@", pass);
How do I then get that *pass string into a UILabel called *nameone?
I've tried all kinds on versions based around
nameone.text = pass
But nothing seems to work, I just get a blank label.
Any help would be brilliant.
Many Thanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks... Deny...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome