Reputation: 113
I'm just starting with swift to program for IOS. I'm already did an app for Android that consume some web services. I need to do the same app, but now for IOS. I already could call the web service correctly and print the response, but I need to access to the properties that the web service return. Ahh, I have various web service and some of them return string arrays. This is the code I'm using to call the services
var lobj_Request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
var session = NSURLSession.sharedSession()
var err: NSError?
var user = nameTextField.text
var pass = passTextField.text
lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue(hostString, forHTTPHeaderField: "Host")
lobj_Request.addValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(soapMessage.characters.count), forHTTPHeaderField: "Content-Length")
lobj_Request.addValue(SOAP_ACTION, forHTTPHeaderField: "SOAPAction")
var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")
var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")
if error != nil
{
print("Error: " + error!.description)
}
})
task.resume()
I know the properties are in data (obviously), but I don't know how to access to them.
Upvotes: 0
Views: 4869
Reputation: 113
I found a very good library to solve this issue. It's call SWXMLHash. https://github.com/drmohundro/SWXMLHash I only did this:
var lobj_Request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
var session = NSURLSession.sharedSession()
var err: NSError?
var user = nameTextField.text
var pass = passTextField.text
lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue(hostString, forHTTPHeaderField: "Host")
lobj_Request.addValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(soapMessage.characters.count), forHTTPHeaderField: "Content-Length")
lobj_Request.addValue(SOAP_ACTION, forHTTPHeaderField: "SOAPAction")
var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")
var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")
var xml = SWXMLHash.parse(data!)
var propertyIwant = xml["soap:Envelope"]["soap:Body"]["SoapResponse"]["SomeField"]["propertyIwant"].element?.text
if error != nil
{
print("Error: " + error!.description)
}
})
task.resume()
Upvotes: 2
Reputation: 1
If your web service returns the string with JSON, you'll need to parse it. Please consider : https://www.hackingwithswift.com/example-code/system/how-to-parse-json-using-nsjsonserialization
And if it's XML string : http://dubinski.org/wpis/easy-xml-parsing-in-swift/
I meant the "strData".
Upvotes: 0