Reputation: 11
im very new at Swift programming (im a cellular biologist) but I managed to get a pretty decent app running using Swift 1.2. The app connects to one of my lab's LIMS systems via webservices and performs various tasks such as reenabling users, fetching HTML and fetching PDFs.
I decided to upgrade to Xcode 7 beta and make my app futureproof but sadly im way out of my element. I managed to fix most of the issues but I cant get the webservice part to function . Could anyone perhaps point me in the right direction. Below is a snippet of code thats responsible for the webservice part of what im doing.
Apparently NSURLConnection does work anymore so Im guessing thats the main thing that needs to change. Any help will be much appreciated! I dont want all my work to go to waste because of Swift 2.0
var mutableData:NSMutableData = NSMutableData.alloc()
var currentElementName:NSString = ""
override func viewDidLoad() {
super.viewDidLoad()
currentws = ""
let soapMessage = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:web='http://www.labware.com/webservice'><soapenv:Header/><soapenv:Body><web:invoke><!--Optional:--><web:authToken>\(authCode)</web:authToken></web:invoke></soapenv:Body></soapenv:Envelope>"
let urlString = webAddress + "DEMO_USERS"
let url = NSURL(string: urlString)
let theRequest = NSMutableURLRequest(URL: url!)
let msgLength = String(soapMessage.characters.count)
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
theRequest.timeoutInterval = 30
let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
connection?.start()
if (connection == true) {
let mutableData : Void = NSMutableData.initialize()
}
test.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
mutableData.length = 0;
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
mutableData.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
let xmlParser = NSXMLParser(data: mutableData)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
}
// NSXMLParserDelegate
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElementName = elementName
}
func parser(parser: NSXMLParser, foundCharacters string: String?) {
if currentElementName == "ns:return" {
if (currentws == "") {
userString = string!
activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
//println(authCode)
test.reloadData()
} else {
reactstatus = "User Reactivated"
let alert = UIAlertController(title: "Success", message: "User : \(userName) successfully reactivated!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
} else if currentElementName == "faultstring" {
activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
let alert = UIAlertController(title: "Alert", message: string, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Upvotes: 1
Views: 850
Reputation: 1417
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "webserviceLink")!)
request.HTTPMethod = "POST"
let data = ""
request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
let task = session.dataTaskWithRequest(request, completionHandler: {(data,response,error) in
do
{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
let arrayJSON = resultJSON as! NSArray
// self.arrPerson = arrayJSON
}
catch
{
print("Recieved not- well Formatted JSON")
}
})
task.resume()
Upvotes: 0
Reputation: 31
Okay so you'll need to get rid of connection methods, and using connection in eveything, and youll need to use a session instead. I did get mine to work. You can still parse it exactly the way you have it, there is no problem with that. Just use a session not a connection
Upvotes: 1