gpt4 book ai didi

ios - 如何使用 alamofire 管理器在 ios app swift4 中添加 ssl 证书固定?

转载 作者:行者123 更新时间:2023-11-28 13:49:39 24 4
gpt4 key购买 nike

我的 ios 应用程序中的 ssl 证书固定有问题,我想要的是使用 alamofire 向所有请求添加 ssl 固定证书,所以下面是我的 alamofire 管理器代码,当我运行该应用程序时,我总是会收到此错误:

load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://myWebsite.com/token, NSErrorFailingURLKey=https://myWebsite.com/token, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<1>, NSLocalizedDescription=cancelled} [-999]

class AFManager : NSObject{

private var sessionManager: SessionManager?


required override init() {
super.init()

enableCertificatePinning()

}

private func enableCertificatePinning() {
let certificates = getCertificates()
let trustPolicy = ServerTrustPolicy.pinCertificates(
certificates: certificates,
validateCertificateChain: true,
validateHost: true)
let trustPolicies = [ "myWebsite.com": trustPolicy ]
let policyManager = ServerTrustPolicyManager(policies: trustPolicies)
sessionManager = SessionManager(
configuration: .default,
serverTrustPolicyManager: policyManager)
}

private func getCertificates() -> [SecCertificate] {
let url = Bundle.main.url(forResource: "myWebsitessl", withExtension: "cer")!
let localCertificate = try! Data(contentsOf: url) as CFData
guard let certificate = SecCertificateCreateWithData(nil, localCertificate)
else { return [] }

return [certificate]
}



///without headers (post)
//used this to registration
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
URLCache.shared.removeAllCachedResponses()
self.init().sessionManager?.request(strURL, method: .post, parameters: params, encoding: URLEncoding.httpBody).responseJSON { (responseObject) -> Void in

//print(responseObject)

if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}


///// response string (post)
//used this in login // used in change password
class func strRequestPOSTURL(_ strURL : String, params : [String : String]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
URLCache.shared.removeAllCachedResponses()
self.init().sessionManager?.request(strURL, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response) in
//print(response)

if response.result.isSuccess {
let resJson = JSON(response.result.value!)
success(resJson)
}
if response.result.isFailure {
let error : Error = response.result.error!

failure(error)
}

}

}

}

我的代码是这样的,请参见下面的代码,它可以正常工作,但是我想要证书固定以确保安全,所以我在上面的代码中添加了一些东西(比如 sessionManager、init()、enableCertificatePinning() 和 getCertificates())然后它就不起作用了

class AFManager : NSObject{


///without headers (post)
//used this to registration
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
URLCache.shared.removeAllCachedResponses()
Alamofire.request(strURL, method: .post, parameters: params, encoding: URLEncoding.httpBody).responseJSON { (responseObject) -> Void in

//print(responseObject)

if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}


///// response string (post)
//used this in login // used in change password
class func strRequestPOSTURL(_ strURL : String, params : [String : String]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
URLCache.shared.removeAllCachedResponses()
Alamofire.request(strURL, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response) in
//print(response)

if response.result.isSuccess {
let resJson = JSON(response.result.value!)
success(resJson)
}
if response.result.isFailure {
let error : Error = response.result.error!

failure(error)
}

}

}

}

我通过将证书拖放到项目中将其添加到我的项目中,请帮忙,我觉得我的代码有一些错误提前致谢

最佳答案

错误 -999最常见于您的 SessionManager 时是deinit 'd 在被使用时,导致您的所有请求被取消。从您的代码看来您没有使用 sessionManager您使用自定义创建的变量 ServerTrustPolicy根本没有,即使你是,也可能是 AFManager 的任何实例您创建的超出范围,导致 deinit .

关于ios - 如何使用 alamofire 管理器在 ios app swift4 中添加 ssl 证书固定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com