此处两个方法都值对服务端进行认证 如果需要客户端认证 请看参考链接
*当前环境为 swift3.0 Xcode8.1
*使用pod 'Alamofire', '~> 4.0'
后台提供cer 后者crt证书(注意crt证书也是需要转为cer证书才能使用)
Alamofire 适配https
在AppDelegate 中添加方法
//配置 网络请求
func configureAlamofireManager() {
//认证相关设置
let manager = SessionManager.default
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
//认证服务器证书
if challenge.protectionSpace.authenticationMethod
== NSURLAuthenticationMethodServerTrust {
print("服务端证书认证!")
let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
let remoteCertificateData
= CFBridgingRetain(SecCertificateCopyData(certificate))!
let cerPath = Bundle.main.path(forResource: "opdj", ofType: "cer")!
let cerUrl = URL(fileURLWithPath:cerPath)
let localCertificateData = try! Data(contentsOf: cerUrl)
if (remoteCertificateData.isEqual(localCertificateData) == true) {
let credential = URLCredential(trust: serverTrust)
challenge.sender?.use(credential, for: challenge)
return (URLSession.AuthChallengeDisposition.useCredential,
URLCredential(trust: challenge.protectionSpace.serverTrust!))
} else {
return (.cancelAuthenticationChallenge, nil)
}
}// 其它情况(不接受认证)
else {
print("其它情况(不接受认证)")
return (.cancelAuthenticationChallenge, nil)
}
}
}
在方法中 调用 如下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
configureAlamofireManager()
return true
}
至此 所有通过Alamofire的网络请求 都会携带服务端证书的认证进行请求
WKWebView 适配https
- info配置App Transport Security Settings -> NSAllowsArbitraryLoadsInWebContent = YES
- 需要WKWebView 添加WKNavigationDelegate 的代理
self.wkWebView.navigationDelegate = self
- 代理方法
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
//认证服务器证书
if challenge.protectionSpace.authenticationMethod
== (NSURLAuthenticationMethodServerTrust) {
print("服务端证书认证!")
let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
let remoteCertificateData
= CFBridgingRetain(SecCertificateCopyData(certificate))!
let cerPath = Bundle.main.path(forResource: "opdj",
ofType: "cer")!
let localCertificateData = NSData(contentsOfFile:cerPath)!
if (remoteCertificateData.isEqual(localCertificateData as Data) == true) {
let credential = URLCredential(trust: serverTrust)
challenge.sender?.use(credential,
for: challenge)
completionHandler(.useCredential,
URLCredential(trust: challenge.protectionSpace.serverTrust!))
} else {
completionHandler(.cancelAuthenticationChallenge, nil)
}
}
// 其它情况(不接受认证)
else {
print("其它情况(不接受认证)")
completionHandler(.cancelAuthenticationChallenge, nil);
}
}
后台不提供证书
Alamofire 适配https
在AppDelegate 中添加方法
//配置 网络请求
func configureAlamofireManager() {
//认证相关设置
let manager = SessionManager.default
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
//无证书 认证
if challenge.protectionSpace.authenticationMethod
== NSURLAuthenticationMethodServerTrust {
ZNLog("服务端证书认证!")
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
return (.useCredential, credential)
}else {
ZNLog("其它情况(不接受认证)")
return (.cancelAuthenticationChallenge, nil)
}
}
}
在方法中 调用 如下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
configureAlamofireManager()
return true
}
至此 所有通过Alamofire的网络请求 都使用https进行请求
wkWebView配置
- info配置App Transport Security Settings -> NSAllowsArbitraryLoadsInWebContent = YES
- 需要WKWebView 添加WKNavigationDelegate 的代理
self.wkWebView.navigationDelegate = self
- 代理方法
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
//认证服务器证书
if challenge.protectionSpace.authenticationMethod
== (NSURLAuthenticationMethodServerTrust) {
print("服务端证书认证!")
let card = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(URLSession.AuthChallengeDisposition.useCredential,card)
}else {
print("其它情况(不接受认证)")
completionHandler(.cancelAuthenticationChallenge, nil);
}
}
SDWebImage访问HTTPS站点获取图片资源失败解决办法
字数39 阅读1000 评论4 喜欢8
最简单的粗暴的方法:
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
options : SDWebImageAllowInvalidSSLCertificates
直接跳过验证证书就可以啦!
参考链接:
网友评论