先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/

ios中使用https,主要就是使用NSURLCredential,先看些必要的官方文档:

NSURLCredential is an immutable object representing an authentication credential consisting of authentication information specific to the type of credential and the type of persistent storage to use, if any.

The URL loading system supports three types of credentials: password-based user credentials, certificate-based user credentials, and certificate-based server credentials (used when verifying the server’s identity).

When you create a credential, you can specify that it should be used for a single request, persisted temporarily (until your app quits), or persisted permanently (in the keychain).

虽然credentials 有许多种,但是针对URL来说,只有3种!

因此有3个初始化方法:

+ credentialForTrust:
+ credentialWithUser:password:persistence:
+ credentialWithIdentity:certificates:persistence:

另外,要参看URL Session Programming Guide中的认证部分:

To attempt to authenticate, the application should create an NSURLCredential object with authentication information of the form expected by the server. You can determine the server’s authentication method by calling authenticationMethod on the protection space of the provided authentication challenge. Some authentication methods supported by NSURLCredential are:

HTTP basic authentication (NSURLAuthenticationMethodHTTPBasic) requires a user name and password. Prompt the user for the necessary information and create an NSURLCredential object with credentialWithUser:password:persistence:.


HTTP digest authentication (NSURLAuthenticationMethodHTTPDigest), like basic authentication, requires a user name and password. (The digest is generated automatically.) Prompt the user for the necessary information and create an NSURLCredential object with credentialWithUser:password:persistence:.


Client certificate authentication (NSURLAuthenticationMethodClientCertificate) requires the system identity and all certificates needed to authenticate with the server. Create an NSURLCredential object with credentialWithIdentity:certificates:persistence:.


Server trust authentication (NSURLAuthenticationMethodServerTrust) requires a trust provided by the protection space of the authentication challenge. Create an NSURLCredential object with credentialForTrust:.
After you’ve created the NSURLCredential object: For NSURLSession, pass the object to the authentication challenge’s sender using the provided completion handler block.
For NSURLConnection and NSURLDownload, pass the object to the authentication challenge’s sender with useCredential:forAuthenticationChallenge:.

在使用AFNetworking3.0时,如果你访问的是自签名的https地址,那么会要求你把网站的自签名证书加入到工程里,用来验证网站证书。如果你没有这个证书,会报错:

In order to validate a domain name for self signed certificates, you MUST use pinning.

这个pinning,指的是证书锁定,意思就是只有client包含的证书和服务器的证书一致时,才能通过验证。

AFnetworking 3.0 好像默认会去 程序中寻找所有cer文件,并找符合要求的。也有准确指定的方法:

    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    securityPolicy.allowInvalidCertificates = YES;

 NSString *certificatePath = [[NSBundle mainBundle] pathForResource:@"tomcat" ofType:@"cer"];
NSData *certificateData = [NSData dataWithContentsOfFile:certificatePath]; NSSet *certificateSet = [[NSSet alloc] initWithObjects:certificateData, nil];
[securityPolicy setPinnedCertificates:certificateSet]; manager.securityPolicy = securityPolicy;

这样做的目的就是安全。

系统自带的nsurlsession的证书验证没有这么严格,它把更多的验证任务交给了程序员,如果你自己不去调用其他函数加强验证,系统也不会强制你使用!

ios 中使用https的知识的更多相关文章

  1. iOS 中对 HTTPS 证书链的验证

    这篇文章是我一边学习证书验证一边记录的内容,稍微整理了下,共扯了三部分内容: HTTPS 简要原理: 数字证书的内容.生成及验证: iOS 上对证书链的验证. HTTPS 概要 HTTPS 是运行在 ...

  2. iOS中XML的相关知识

    1.什么是XML “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴.有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它不 ...

  3. iOS中的HTTPS

    在WWDC 15,Apple提出的ATS (App Transport Security) ,是 Apple 在推进网络通讯安全的一个重要方式.在 iOS 9 和 OS X 10.11 中,默认情况下 ...

  4. iOS 中 AFNetworking HTTPS 的使用

    由于我们公司由HTTP转HTTPS,出现了一系列问题特此记录下. 一.HTTPS 二.App Transport Security 三.iOS 中用HTTPS 注意的问题 四.使用 AFNetwork ...

  5. iOS中多线程知识总结(一)

    这一段开发中一直在处理iOS多线程的问题,但是感觉知识太散了,所以就把iOS中多线程的知识点总结了一下. 1.基本概念 1)什么是进程?进程的特性是什么? 进程是指在系统中正在运行的一个应用程序.   ...

  6. 【腾讯Bugly干货分享】iOS 中 HTTPS 证书验证浅析

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/-fLLTtip509K6pNOTkflPQ 导语 本 ...

  7. Android 和iOS 中关于View 的一点知识

    View的概念和方法十分重要,这里将对Android 和iOS中出现的,关于视图的一些知识点进行总结,预计文章会比较长,要许多时间慢慢补充. 先转载一部分资料,感谢原作者! 原链接为:http://b ...

  8. Cordoval在iOS中的运用整理

    一:关于Cordoval理论知识 1:PhoneGap是手机平台上流行的一款中间件.它构建在各种手机平台所提供的WebView(浏览器内核)组件的基础之上,使用javascript语言对应用开发者提供 ...

  9. iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)

    本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...

随机推荐

  1. XMLHttpRequestUtil

    //XMLHttpRequest类 function XMLHTTPRequestUtil() { this.Instance = null; this.GetXMLHttpRequest = fun ...

  2. [Exchange]使用EWS托管API2.0同步邮箱

    你可以通过Exchange Web Serivice(EWS)托管API去检索从一个给定的时间点,文件夹中有变化的列表中的项. 客户端可以使用SyncFoldersItems方法,同步服务端的项目,你 ...

  3. 解决redhat的未注册问题

    昨天安装第五步的时候:开始是没有网,,,居然ping不通  网  ,服务器也ping不通,,,,,可能和我前几天删除了网络适配器有关,,把linux桥接对应的适配器给删了,,, 解决办法是打开虚拟网络 ...

  4. linux 文件操作和权限

    1.touch 创建文件 2.查看文件cat 浏览一个较短文件,行号加上cat -n 3.反向显示内容tac 并不支持-n选项 4.分页显示文件内容more  空格或f 翻页  回车换行  q或者Q退 ...

  5. PHP数据类型转换

    转自:http://www.tianzhigang.com/article.asp?id=280 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: (int).(integer):转换成 ...

  6. standford工具-parser

    stanford自然语言处理开源了很多工具,很实用也很方便,记录下来,以备后用. 第一篇就从句法分析开始吧(所用的平台都是java+eclipse). <一>操作 1.http://www ...

  7. [译]git commit --amend

    git commit --amend命令用来修复最近一次commit. 可以让你合并你缓存区的修改和上一次commit, 而不是提交一个新的快照. 还可以用来编辑上一次的commit描述. 记住ame ...

  8. linux压缩排除

    tar -zcvf www/la.tar.gz --exclude=www/uploadfile --exclude=www/databases --exclude=www/web_logs www ...

  9. Redis数据库的使用与介绍

    本周11-15号开始用Redis数据库在现有的平台基础上开发一个独立模块,这是一个边学习.边记录.边交流.边开发.边总结的过程.大部分随笔都是个人的“工作日志”,旨在记录自己学习过程中收集的一些资料, ...

  10. Swift3.0P1 语法指南——基本操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...