BJM
BJM

Reputation: 233

Consume a SOAP WebService with WSDL

I have done a tutorial for using a SOAP WeService in SWIFT:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

    var mutableData: NSMutableData = NSMutableData.alloc()
    var currentElementName: NSString = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var soapMessage = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><CelsiusToFahrenheit xmlns='http://www.w3schools.com/webservices/'><Celsius>0</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"



        var urlString = "http://www.w3schools.com/webservices/tempconvert.asmx"

        var url = NSURL(string: urlString)
        var theRequest = NSMutableURLRequest(URL: url!)
        var msgLength = String(count(soapMessage))


        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
        connection?.start()
        if(connection == true) {
            var mutableData : Void = NSMutableData.initialize()
        }
    }



    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        mutableData.length = 0;
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
        mutableData.appendData(data)
    }


    func connectionDidFinishLoading(connection: NSURLConnection!) {
        var xmlParser = NSXMLParser(data: mutableData)
        xmlParser.delegate = self
        xmlParser.parse()
        xmlParser.shouldResolveExternalEntities = true

    }

    func parser(parser: NSXMLParser,
        didStartElement elementName: String,
        namespaceURI: String?,
        qualifiedName qName: String?,
        attributes attributeDict: [NSObject : AnyObject]) {
        currentElementName = elementName
    }

    func parser(parser: NSXMLParser, foundCharacters string: String?) {
        if currentElementName == "CelsiusToFahrenheitResult" {
            println(string)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

It's works, I get the temperature converted, I want to implement this on my web service, but I don't know how to do, I have:

But I don't know how to do because in the tutorial I put the xml in a variable, and where I put the server url, and how to call a function of the wsdl file ?

Upvotes: 0

Views: 1752

Answers (1)

Syed Absar
Syed Absar

Reputation: 2284

For Swift 2.0, you can generate a Soap Client for your WSDL online at: http://wsdl2swift.com and then utilise the Operations by creating Request Objects:

enter image description here

Upvotes: 1

Related Questions