我使用此代码绕过CA:
import Alamofire
import SwiftyJSON
import Reqres
class Manager{
static let sharedInstance: Manager = Manager()
static let manager:SessionManager = {
let configuration = Reqres.defaultSessionConfiguration()
let manager = SessionManager()
return manager
}()
}
class HttpsRequest : UIViewController {
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 request(url : String , type : String) {
Manager.manager.request(url,method:.get ).responseString{
response in
if response.result.isSuccess{
print("response.value.result?)")
}
else{
print("EROR:\(String(describing: response.result.error))")
}
}
}
func requestAndResponse (type : String , user : String , pass : String) -> String{
let url = "some url"
self.setByPass()
request(url: url, type: type)
}
}
,在另一个类中,我称为“ requestAndResponse”函数,以发送https请求。像这样的东西:
@IBAction func viewprofile(_ sender: Any) {
objHttpsrequest.requestAndResponse(type: type, user : dataForSend1 , pass : dataForSend2)
let viewProfile = self.storyboard?.instantiateViewController(withIdentifier: "ViewProfile") as! ViewProfile
self.navigationController?.pushViewController(viewProfile,animated: true)
}
}
}
绕过CA可以正常工作,但会延迟。在IBAction中是指先发送Https请求然后转到另一个视图,然后再转到另一个View然后再发送一个https请求。