Created
January 21, 2015 12:58
-
-
Save dethbiscuit/af50baf888ed25479644 to your computer and use it in GitHub Desktop.
Swift and Soap Web Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Call a soap web service | |
// | |
// Add | |
// NSURLConnectionDelegate | |
// NSXMLParserDelegate | |
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, NSURLConnectionDelegate, NSXMLParserDelegate{ | |
@IBOutlet weak var mapKit: MKMapView! | |
var wsUrl : String = "http://mobilewebservice.greenmotion.ch/MobileEvpass.asmx" | |
var mutableData:NSMutableData = NSMutableData.alloc() | |
var currentElementName:NSString = "" | |
var lastElementName:NSString = "" | |
let lastNodeName = "Latitude" | |
let firstNodeName = "Longitude" | |
var chargingStation = ChargingStation() | |
var chargingStations = [ChargingStation]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Web services | |
var date = NSDate().ToShortDateTime() | |
var password = "656214CDD7C0782AF86531326D0AE048" | |
var hash = (password+date).MD5() | |
var methodName = "GetChargingStationsList" | |
var userId = 1 | |
var text = String(format: "<?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><%@ xmlns='http://tempuri.org/'><userId>%d</userId><date>%@</date><hash>%@</hash></%@></soap:Body></soap:Envelope>", methodName, userId, date, hash, methodName) | |
var soapMessage = text | |
var url = NSURL(string: wsUrl) | |
var theRequest = NSMutableURLRequest(URL: url!) | |
var msgLength = String(countElements(soapMessage)) | |
theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length") | |
theRequest.HTTPMethod = "POST" | |
theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false | |
var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true) | |
connection?.start() | |
if(connection == true){ | |
var mutableData : Void = NSMutableData.initialize() | |
} | |
} | |
// MARK: - Web Service | |
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) { | |
mutableData.length = 0; | |
} | |
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) { | |
mutableData.appendData(data) | |
} | |
// Parse the result right after loading | |
func connectionDidFinishLoading(connection: NSURLConnection!) { | |
println(mutableData) | |
var xmlParser = NSXMLParser(data: mutableData) | |
xmlParser.delegate = self | |
xmlParser.shouldProcessNamespaces = false | |
xmlParser.shouldReportNamespacePrefixes = false | |
xmlParser.shouldResolveExternalEntities = false | |
xmlParser.parse() | |
} | |
// NSXMLParserDelegate | |
// Operation to do when a new element is parsed | |
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) { | |
if(elementName != nil){ | |
lastElementName = currentElementName | |
currentElementName = elementName | |
if(elementName == firstNodeName){ | |
chargingStation = ChargingStation() | |
} | |
} | |
println(String(format : "didStartElement / elementName %@", elementName)) | |
} | |
// Operations to do for each element | |
func parser(parser: NSXMLParser!, foundCharacters string: String!) { | |
println(String(format : "foundCharacters / value %@", string)) | |
if(string != nil){ | |
if currentElementName == "Longitude" { | |
chargingStation.longitude = (string as NSString).doubleValue | |
} | |
if currentElementName == "Latitude" { | |
chargingStation.latitude = (string as NSString).doubleValue | |
} | |
} | |
} | |
// Add the data to a mutable array of objects | |
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) { | |
println(String(format : "didEndElement / elementName %@", elementName)) | |
if(elementName == lastNodeName){ | |
chargingStations.append(chargingStation) | |
} | |
} | |
// Operations to do when the result has been parsed | |
func parserDidEndDocument(parser: NSXMLParser!) { | |
println("parserDidEndDocument") | |
println(chargingStations.count) | |
println("parserDidEndDocument") | |
addAnnotations() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To where did you sent the parsed response? Thats what I dont get.