Post marked as solved
Click to stop watching this thread.
You have stopped watching this post. Click to start watching again.
contentPostList.repliessolved.tooltip
Replied In
How to use URLSession
I think I finally found a workable solution. I kept searching and found some code that uses semaphores and incorporated it into my original function and it seems to work.
func doHttpCall(st : String) -> String {
var xmlStr = ""
let url = URL(string: st)!
let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
if let error = error {
print("Error: \(error)")
} else if let response = response as? HTTPURLResponse,
300..<600 ~= response.statusCode {
print("Error: \(response.statusCode)")
} else {
let mydata = data!
xmlStr = String(data: mydata, encoding: .utf8)!
print(xmlStr)
}
semaphore.signal()
})
task.resume()
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
return xmlStr
}
And I even understand what it's doing.
If you have a better solution, I'll always be happy to see it. But sending an http request and getting a response is just an incidental but necessary part of my little app. It only took me a week to find a solution. Thanks again for your trouble.