How to implement SOAP Web service with SWIFT 4.0 programming language?

Update (13 November, 2017): Updated codes for Xcode 9 and swift 4.0

Update (25 March, 2016): I’ve updated codes for Xcode 7 and swift 2.0

Update (16 September, 2016): Code updated for Xcode 7.2.1 and swift 2.1.1

Today I’ll show you how to develop SWIFT based application that communicates with a web service using SOAP.

We’ll use following  web service and convert temperature from celcious to fahrenheit:

http://www.webservicex.net/ConvertTemperature.asmx

If you are new to SOAP, then I’d recommend you to learn it from the following link:

http://www.w3schools.com/webservices/ws_soap_intro.asp

And to learn the basics of swift, you can read apple’s documentation by clicking here.

Requirements: Basic understanding of Web services, SOAP and swift.

First open Xcode 9 (or later version) and create a new project named “Temperature Converter”. Select swift as language from first dialog box. Created project will contain ViewController.swift, Main.storyboard and other files.

Screen Shot 2014-10-24 at 11.23.03 PM
Open Main.storyboard and add textfield, labels and a buttons as shown in the screenshot below.
Screen Shot 2014-10-24 at 11.30.28 PM
Add outlets of textfields and add an action for the button. If the image below is too small to view, please click on it to see the full size. In case if you don’t know how to do this, open storyboard and swift file side by side in Assistant editor view. Ctrl + Drag and drop textfields to the swift file. Do the same thing for action, just change the connection type from “Outlet” to “Action” in popup box. I gave the outlet names txtCelcius and txtFahrenheit. For button’s action name I used actionConvert. You can use any name. I didn’t add any outlet for button.
Screen Shot 2014-10-24 at 11.31.55 PM
Now let’s change the codes step by step to add SOAP service with this application.

Step 1: Add delegation names that we’ll be delegating through out this project. We’ll delegate:UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate.

 

Step 2: We need some variables to store received data from server and keep track of those data. Let’s declare them.

 

Step 3: Modify the actionConvert method to let it get user input from Celcius textfield and make a request to server using post method. The full actionConvert method will look like following code:

 


 

Step 4: Using following 3 methods we’ll delegate with NSURLConnection. First 2 methods will save data received from server in mutableData variable. The last method will start NSXMLparser to parse SOAP message.

 

Step 5: We need 2 methods to parse SOAP message returned from server. I’ll discuss one by one so that you can understand them.

The first method is called each time a new node or element of SOAP (or XML) is parsed. We need to  save this name so that we can detect each node in second method. Let’s see the first method:

 

 

The second method will check the current element name and if it finds “ConvertTempResult” element, then it displays the value in Fahrenheit text field.

Now, save and run the code. If you put any numeric value in Celsius box and tap Convert button, Fahrenheit box will display the result fetched from server.

 

iOS Simulator Screen Shot Oct 27, 2014, 7.33.25 PM

 

You can download the whole project from here.

Or can pull the latest project code from Github:

https://github.com/rubelbd82/soapwithswift

Best of luck!!!