In case if any one needs here is my network class..
import UIKit
import Alamofire
import Reqres
class Manager{
static let sharedInstance: Manager = Manager()
static let manager:SessionManager = {
let configuration = Reqres.defaultSessionConfiguration()
let manager = SessionManager()
return manager
}()
// Call this function before each get/post request only if you have unsigned certificate for https request
func setByPass() {
let delegate: SessionDelegate = Manager.manager.delegate
delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .cancelAuthenticationChallenge
} else {
credential = Manager.manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if credential != nil {
disposition = .useCredential
}
}
}
return (disposition, credential)
}
}
func getRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
Manager.manager.request(url, method: .get, parameters: param, encoding: URLEncoding.default).responseJSON { (response:
DataResponse<Any>) in
guard response.result.isSuccess else{
Utils.hideHUD()
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {
})
return
}
Utils.hideHUD()
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
success(response.result.value! as! Dictionary<String, Any>)
}
}
func getRequestWithoutParam(url:URL,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, encoding:JSONEncoding.default, headers: headers).responseJSON { (response:
DataResponse<Any>) in
guard response.result.isSuccess else{
Utils.hideHUD()
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {
})
return
}
Utils.hideHUD()
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
success(response.result.value! as! Dictionary<String, Any>)
}
}
func postRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, method: .post, parameters: param, encoding:JSONEncoding.default, headers: headers).responseJSON { (response:
DataResponse<Any>) in
guard response.result.isSuccess else{
Utils.hideHUD()
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {
})
return
}
Utils.hideHUD()
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
success(response.result.value! as! Dictionary<String, Any>)
}
}
func putRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, method: .put, parameters: param, encoding:JSONEncoding.default, headers: headers).responseJSON { (response:
DataResponse<Any>) in
guard response.result.isSuccess else{
Utils.hideHUD()
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {
})
return
}
Utils.hideHUD()
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
success(response.result.value! as! Dictionary<String, Any>)
}
}
}