Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to handle a client for SOAP based web service in iOS?

user-image
Question added by Muhammad Shahid Janjua , Principal Engineer , HT Decisions
Date Posted: 2017/01/19
Mubin Mall
by Mubin Mall , Sr. iOS Application Developer , Global Gene Corp

import UIKit

 

class ViewController: UIViewController {

var is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\\"http://schemas.xmlsoap.org/soap/envelope/\\" xmlns:cgs=\\"http://YOURURL/\\"><soapenv:Header/><soapenv:Body><cgs:PATHTOAPPENDINURL/></soapenv:Body></soapenv:Envelope>"

 

override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

}

 

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

}

 

@IBAction func btnClicked(sender: AnyObject)

{

    var is_URL: String = "http://www.cgsapi.com/CGSWebService.asmx"

 

    var lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)

    var session = NSURLSession.sharedSession()

    var err: NSError?

 

    lobj_Request.HTTPMethod = "POST"

    lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)

    lobj_Request.addValue("www.cgsapi.com", forHTTPHeaderField: "Host")

    lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")

    lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")

    //lobj_Request.addValue("", forHTTPHeaderField: "Content-Length")

    lobj_Request.addValue("http://www.cgsapi.com/GetSystemStatus", forHTTPHeaderField: "SOAPAction")

 

    var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in

        println("Response: \\(response)")

        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)

        println("Body: \\(strData)")

 

        if error != nil

        {

            println("Error: " + error.description)

        }

 

    })

    task.resume()

}

 

}

 

 

Before answering, I'm going to give a brief overview for the benefit of the wider audience; experts, bear with me please :)

 

What is a Web Service?

In short, a Web Service is an API (Application Programming Interface) which is accessible via HTTP.  

 

How does a Web Service function?

  • XML-formatted object(s) are exchanged via HTTP
  • XML being the definition language used to format the object, and the reason is that it is a standard
  • HTTP is the most widely used Internet protocol, and it is also a standard

SOAP?

SOAP stands for Simple Object Access Protocol.  It is an XML-based protocol for accessing web services and allowing applications to exchange information over HTTP.

 

And now for the answer: how to handle SOAP in iOS.  To do that from the iOS SDK, below are the steps:

 

  1. Create a string with your SOAP request.  This will be your access URL
  2. Crate an NSMutableRequest with your request URL formatted in (1) above
  3. Get your XML response in an NSURLConnections delegate methods
  4. Parse the XML response.  You can use libraries such as KissXML, TBXML or whatever you prefer to achieve your parsing; unless you are a total geek accustomed to pain, insistent on writing an XML parser yourself, in which case you are much braver than ! :)

Hope this helps ..

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.