I have researched the last few hours and cannot find an example that works in iOS 9 in my Swift application. I simply want to do an HTTP GET request to a url "http://www.example.com". The URL will return XML data that will be parsed but for now I just want to get the HTTP GET command to run.
I am new to Xcode but have some programming background but cannot figure out how to get a URL to do a GET command
I have a button that I have and I want it to run the HTTP GET request when the button is pressed. When looking through the NSURLSession examples online, I cannot find one that won't throw a syntax error. Any help would be appreciated. Below is one I found but its throwing a bunch of errors. Any guidance would be appreciated.
import Foundation
import XCPlayground
func httpGet(request: NSURLRequest!, callback: (String, String?) -> Void) {
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if error != nil {
callback("”, error.localizedDescription),
} else {
var result = NSString(data: data, encoding:
NSASCIIStringEncoding)!
callback(result, nil)
}
}
task.resume()
}
var request = NSMutableURLRequest(URL: NSURL(string: “http://www.google.com")!)
httpGet(request){
(data, error) -> Void in
if error != nil {
println(error)
} else {
println(data)
}
}
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)