How can I return just a NSHttpUrlResponse status code from NSUrlSession. I am very new to this language.So,If there is other better practice please let me know.My code is right below here.
//Method to return a status code
func responseCode() -> Int{
var responseCode : Int=0
if Reachability.isConnectedToNetwork() == true {
// get news feed url
let url = NSURL(string: baseUrl)
let session = NSURLSession.sharedSession()
//create request
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = httpMethod
//check null value
if (bodyData != "") {
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
}
if (auth != "") {
print("Token Auth : \(auth)")
request.addValue("bearer \(self.auth)", forHTTPHeaderField: "Authorization")
}
print("request : \(request)")
//How to return a Integer from this below function
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
print("Error\(error)")
if let httpResponse = response as? NSHTTPURLResponse {
print("HttpReponse\(httpResponse.statusCode)")
// Get a status code and assign it in a variable
responseCode = httpResponse.statusCode
}
})
task.resume()
//Return a response code in ViewController but it just return a initial set value that is 0
return responseCode
}
//Return a responseCode in ViewController but it just return a initial set value that is 0
return responseCode
}