> For the complete documentation index, see [llms.txt](https://yongpenglovemimi123.gitbook.io/henry/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yongpenglovemimi123.gitbook.io/henry/ios/cfnetwork-nsurlsession-nsurlconnection.md).

# CFNetwork NSURLSession NSURLConnection

![request.jpg](https://img.hacpai.com/file/2019/12/request-abb626b6.jpg)

> CFNetworkDemo: <https://github.com/zColdWater/CFNetworkDemo>
>
> 本文项目 Demo: <https://github.com/zColdWater/BetweenSessionAndConnectionDemo>
>
> 参考 WWDC PDF: <https://github.com/zColdWater/Resources/blob/master/Images/707_whats_new_in_foundation_networking.pdf>

## 内容

* iOS 网络层的架构
* NSURLSession 和 NSURLConnection 层级关系？
* 总结

## 一，iOS 网络层的架构

从 CFNetwork 的官方介绍的架构图上可以看到，他们的关系是：

**1.最上层：** UIWebView 和 WKWebView 使用的 WebKit 封装的 NSURL(也就是封装的，NSURLSession，NSURLConnection)

**2.NSURL 层：** NSURLSession 和 NSURLConnection 在这层，也就是我们平时常用的 API，它封装的 CFNetwork。

**3.CFNetwork 层：** CFNetwork 这层的 API 全部是 C 函数，我们平时开发一般情况下肯定不会使用它，因为有接口更有好的 NSURL 层，我们知道它又是封装的 BSD sockets 层

**4.BSD sockets:** 这层就是最底层的 Socket 层，平时开发基本就没接触过这层。

![WechatIMG998.png](https://img.hacpai.com/file/2019/12/WechatIMG998-ae62576f.png)

我们再看一张苹果 WWDC 介绍 Network 架构截图 从中可以看出，URLSession 和 URLConnection 是平级的，但是，你往下看我们验证的关系，似乎在 URLConnection 里面调用了 URLSession，否则为什么我们 Hook URLSession 的构造器，使用 URLConnection 的时候却走了这个方法。

![WechatIMG1015.png](https://img.hacpai.com/file/2019/12/WechatIMG1015-9c3ec7dc.png)

![WechatIMG1016.jpeg](https://img.hacpai.com/file/2019/12/WechatIMG1016-4c57ee23.jpeg)

![WechatIMG1017.jpeg](https://img.hacpai.com/file/2019/12/WechatIMG1017-9091f061.jpeg)

## 二，NSURLSession 和 NSURLConnection 关系？

虽然我们知道了 NSURL 层有两个类，NSURLSession 和 NSURLConnection，但是我们还是不清楚，他们之间的关系，我也是好奇者之一，于是我做了一个 Example 来验证自己的想法。

1.添加类别方法，+load 方法里面做方法交换，来 hook，URLSession 的两个构造器方法，可以忽略 MyURLProtocol 这句，这句是用来 hook ![WechatIMG999.png](https://img.hacpai.com/file/2019/12/WechatIMG999-dcefab22.png)

2.在 ViewController 视图控制器中，使用 URLConnection，请求一条 POST 的请求。 ![image.png](https://img.hacpai.com/file/2019/12/image-391615c0.png)

3.发起请求，然后发现，断点卡在了 URLSession 的便利构造器的方法上面，因为我 hook 了系统的这个方法，所以断点走到了这里。 这个时候我就可以得出一个结论。

结论： 从下图我们可以看出，我们调用了 NSURLConnection，但是却走到了 NSURLSession 的便利构造器里面。所以，我们可以认为，NSURLConnection 封装了 NSURLSession 方法。 ![WechatIMG1001.png](https://img.hacpai.com/file/2019/12/WechatIMG1001-944c1cc3.png)

## 三，总结

经过自己的例子，和官方文档的资料，我们认为 iOS 当中的网络框架结构是这样的。

![WechatIMG1002.png](https://img.hacpai.com/file/2019/12/WechatIMG1002-8f643cf2.png)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yongpenglovemimi123.gitbook.io/henry/ios/cfnetwork-nsurlsession-nsurlconnection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
