docs: Electron

This commit is contained in:
杭城小刘
2020-05-04 02:51:19 +08:00
parent 79f10acba2
commit 8dbcff87ed
42 changed files with 851 additions and 33 deletions

View File

@@ -4,6 +4,10 @@
## 一、 NSURLProtocol 是什么
> An NSURLProtocol object handles the loading of protocol-specific URL data. The NSURLProtocol class itself is an abstract class that provides the infrastructure for processing URLs with a specific URL scheme. You create subclasses for any custom protocols or URL schemes that your app supports.
![URL Loading System](./../assets/2020-04-18-URL-loading-system.png)
NSURLProtocol 是 Foundation 框架中 URL Loading System 的一部分。它可以让开发者可以在不修改应用内原始请求代码的情况下,去改变 URL 加载的全部细节。换句话说NSURLProtocol 是一个被 Apple 默许的中间人攻击。
虽然 NSURLProtocol 叫“Protocol”却不是协议而是一个抽象类。
@@ -324,7 +328,38 @@ didCancelAuthenticationChallenge:challenge];
## 五、 补充内容
## 五、 读源码,学习 NSURLProtocol
iOS 中网络测试框架 [ OHHTTPStubs](https://github.com/AliSoftware/OHHTTPStubs)的实现就是利用了 NSURLProtocol 实现的。
几个主要类及其功能HTTPStubsProtocol 拦截网络请求HTTPStubs 单例管理 HTTPStubsDescriptor 实例对象HTTPStubsResponse 伪造 HTTP 请求。
HTTPStubsProtocol 继承自 NSURLProtocol可以在 HTTP 请求发送之前对 request 进行过滤处理
```objective-c
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
BOOL found = ([HTTPStubs.sharedInstance firstStubPassingTestForRequest:request] != nil);
if (!found && HTTPStubs.sharedInstance.onStubMissingBlock) {
HTTPStubs.sharedInstance.onStubMissingBlock(request);
}
return found;
}
```
`firstStubPassingTestForRequest` 方法内部会判断请求是否需要被当前对象处理
紧接着开始发送网络请求。实际上在 `- (void)startLoading` 方法中可以用任何网络能力去完成请求,比如 NSURLSession、NSURLConnection、AFNetworking 或其他网络框架。OHHTTPStubs 的做法是获取 request、client 对象。如果 HTTPStubs 单例中包含 `onStubActivationBlock` 对象,则执行该 block然后利用 responseBlock 对象返回一个 HTTPStubsResponse 响应对象。
[参考资料](https://github.com/draveness/analyze/blob/master/contents/OHHTTPStubs/如何进行%20HTTP%20MockiOS.md)
## 六、 补充内容
### 1. 使用 NSURLSession 时的注意事项