ResponseDetective 是一个无侵入式的框架,用于拦截应用与服务器之间任何出站请求和入站响应以进行调试。
要求
ResponseDetective 使用 Swift 5.2 编写,并支持 iOS 8.0+、macOS 10.10+ 和 tvOS 9.0+。
使用
将 ResponseDetective 集成到您的项目中非常简单,总共只需两步
步骤 1: 启用拦截
为了使 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 集成到您的代码中只需要初始化您的 AFURLSessionManager
/Manager
并使用上述 (NS)URLSessionConfiguration
// 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
如果您使用 Carthage,请将以下依赖项添加到您的 Cartfile
github "netguru/ResponseDetective" ~> {version}
CocoaPods
如果您使用 CocoaPods,请将以下依赖项添加到您的 Podfile
use_frameworks!
pod 'ResponseDetective', '~> {version}'
关于
该项目由 Netguru 用爱心制作,并由 Adrian Kashivskyy 维护。
版本名称
从版本 1.0.0 开始,ResponseDetective 的版本命名按照 Sherlock Holmes 作品集的时间顺序命名。如果我们达到了 60 个版本,并且没有更多的故事,会发生什么?我们还不知道,也许我们会开始用猫或者其他什么来命名。
许可证
该项目采用 MIT 许可证。有关更多信息,请参阅 LICENSE.md。