ResponseDetective 是一个非侵入式框架,用于拦截你的应用和服务器之间任何发出的请求和传入的响应,以达到调试目的。
ResponseDetective 使用 Swift 5.3 编写,并支持 iOS 9.0+、macOS 10.10+ 和 tvOS 9.0+。
在你的项目中整合 ResponseDetective 非常简单 – 只需要两个步骤
为了使 ResponseDetective 工作,需要将其添加为你的 (NS)URLSession
和互联网之间的中间人。你可以通过在你的会话的 (NS)URLSessionConfiguration.protocolClasses
中注册提供的 URLProtocol
类来实现,或者使用快捷方法
// Objective-C
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[RDTResponseDetective enableInConfiguration:configuration];
// Swift
let configuration = URLSessionConfiguration.default
ResponseDetective.enable(inConfiguration: configuration)
然后,你应该将该配置用于你的 (NS)URLSession
// Objective-C
NSURLSession *session = [[NSURLSession alloc] initWithConfiguration:configuration];
// Swift
let session = URLSession(configuration: configuration)
或者,如果你使用 AFNetworking/Alamofire 作为你的网络框架,整合 ResponseDetective 就简化为仅使用上述 (NS)URLSessionConfiguration
初始化你的 AFURLSessionManager
/Manager
// Objective-C (AFNetworking)
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
// Swift (Alamofire)
let manager = Alamofire.SessionManager(configuration: configuration)
就这样!
现在是执行实际请求的时候了
// Objective-C
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://httpbin.org/get"]];
[[session dataTaskWithRequest:request] resume];
// Swift
let request = URLRequest(URL: URL(string: "http://httpbin.org/get")!)
session.dataTask(with: request).resume()
瞧!🎉 查看你的控制台输出
<0x000000000badf00d> [REQUEST] GET https://httpbin.org/get
├─ Headers
├─ Body
│ <none>
<0x000000000badf00d> [RESPONSE] 200 (NO ERROR) https://httpbin.org/get
├─ Headers
│ Server: nginx
│ Date: Thu, 01 Jan 1970 00:00:00 GMT
│ Content-Type: application/json
├─ Body
│ {
│ "args" : {
│ },
│ "headers" : {
│ "User-Agent" : "ResponseDetective\/1 CFNetwork\/758.3.15 Darwin\/15.4.0",
│ "Accept-Encoding" : "gzip, deflate",
│ "Host" : "httpbin.org",
│ "Accept-Language" : "en-us",
│ "Accept" : "*\/*"
│ },
│ "url" : "https:\/\/httpbin.org\/get"
│ }
如果你使用 Carthage,请将以下依赖项添加到你的 Cartfile
github "netguru/ResponseDetective" ~> {version}
如果你使用 CocoaPods,请将以下依赖项添加到你的 Podfile
use_frameworks!
pod 'ResponseDetective', '~> {version}'
如果你使用 Swift Package Manager,请将此仓库添加到你项目设置中的 Swift Packages。
要安装测试依赖项或构建 ResponseDetective 本身,请不要直接运行 carthage
。它无法处理 Xcode 12 中引入的 Apple Silicon 架构。相反,通过 carthage.sh
脚本运行它
$ ./carthage.sh bootstrap
或者,你可以使用 Swift Package Manager 和以下命令在本地运行测试
$ swift test
此项目由 Netguru 用 ♡ 制作。
从 1.0.0 版本开始,ResponseDetective 的发布版本以 夏洛克·福尔摩斯正典故事 的名称命名,按时间顺序排列。如果我们达到 60 个发布版本并且没有更多故事了会发生什么? 我们不知道,也许我们会开始以猫或其他东西的名字命名。
本项目根据 MIT 许可证 授权。有关更多信息,请参阅 LICENSE.md。