ios https适配

此处两个方法都值对服务端进行认证 如果需要客户端认证 请看参考链接
*当前环境为 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

直接跳过验证证书就可以啦!

参考链接:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 快速适配直接看下面的示例代码吧,概念有点多。。。 自己客户端生成证书放在服务器上,可以自签服务器必须ca签署,服务...
    _YZG_阅读 10,971评论 0 56
  • 类一: 证书自己生成的(一般不这么搞) AFNetWorking 2.0+ 适配 导出.cer 文件后,拷贝如 程...
    苹果农阅读 434评论 1 0
  • 在WWDC2016公告中,苹果建议开发商尽快使用ATS协议。该协议是iOS9和macOS (OS X) 10.11...
    吃屁的小栗子阅读 746评论 0 7
  • 首先,如果你的项目有很多的H5页面,并且用的还不是普通的静态页面,请保持 这样的设置,因为h5页面包含很多,并且有...
    iOS大熊猫阅读 1,189评论 0 1
  • 在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到2017年1月1日 App Store中的所有应用都必...
    o翻滚的牛宝宝o阅读 21,989评论 85 146