美文网首页
ios https适配

ios https适配

作者: 学习之路 | 来源:发表于2016-12-14 11:03 被阅读510次

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

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

参考链接:

相关文章

  • iOS适配HTTPS

    一切为了迎合苹果 在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到2017年1月1日 App Stor...

  • iOS https适配

    首先,如果你的项目有很多的H5页面,并且用的还不是普通的静态页面,请保持 这样的设置,因为h5页面包含很多,并且有...

  • iOS适配Https

    由于苹果是17年的1月1日期审核要求强制开启ATS,所以各个应用的程序员都已经开始了https的适配。下面介绍一下...

  • ios https适配

    此处两个方法都值对服务端进行认证 如果需要客户端认证 请看参考链接*当前环境为 swift3.0 Xcode8...

  • iOS适配HTTPS

    自2017年1月1日起,提交到App Store的所有APP必须遵循ATS(App Transport Secur...

  • iOS 适配Https

    一、简单介绍下info.plist中的配置设置 NSAllowsArbitraryLoadsInMedia (设置...

  • iOS适配https

    http://my.oschina.net/non6/blog/290175

  • iOS https 适配

    http://blog.csdn.net/zhouzhoujianquan/article/details/525...

  • iOS HTTPS适配

    快速适配直接看下面的示例代码吧,概念有点多。。。 SSL/TLS协议运行机制概述图解SSL/TLS协议 一、单向认...

  • iOS10 适配 ATS(app支持https通过App Sto

    iOS10 适配 ATS(app支持https通过App Store审核) iOS10 适配 ATS(app支持h...

网友评论

      本文标题:ios https适配

      本文链接:https://www.haomeiwen.com/subject/hnrimttx.html