I have function to send asynchronous request of a URL on Swift 1.2, but it doesn't work on Swift 2.
func getpost(method:String,bodyt:String,url:String,completion: (res: AnyObject?)->Void){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
let urlString = url
let url:NSURL! = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = method
let postString = bodyt
if method.uppercaseString == "POST"{
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
}
let mainQueue = NSOperationQueue.mainQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{(response, data, error) in
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if (error == nil) && (statusCode == 200){
let resultdata: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil)
dispatch_async(dispatch_get_main_queue(), {
completion(res: resultdata)
})
}
}
}
}
pls help :)