
響應式攻擊性是一個非侵入性的框架,用於攔截應用程序和服務器之間的任何傳出請求以及用於調試目的的響應。
響應式攻擊性用Swift 5.3編寫,並支持iOS 9.0+ , MACOS 10.10+和TVOS 9.0+ 。
在您的項目中納入響應性探測性非常簡單 - 這一切都歸結為兩個步驟:
為了進行響應的工作,需要在您的(NS)URLSession和Internet之間作為中間人添加它。您可以通過在會話的(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用作網絡框架,則集成響應性探索性歸結為僅將您的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"
│ }
如果您使用的是迦太基,請將以下依賴性添加到您的Cartfile :
github "netguru/ResponseDetective" ~> {version}
如果您使用的是Cocoapods,請將以下依賴性添加到您的Podfile :
use_frameworks!
pod 'ResponseDetective', '~> {version}'
如果您使用的是Swift軟件包管理器,請將此存儲庫添加到項目設置中的Swift軟件包中。
要安裝測試依賴項或構建反應性探測性本身,請不要直接運行carthage 。它無法處理Xcode 12中引入的Apple Silicon架構。相反,將其運行carthage.sh腳本:
$ ./carthage.sh bootstrap另外,您可以使用以下命令使用Swift軟件包管理器在本地運行測試:
$ swift test 該項目是由Netguru由Netguru製成的。
從1.0.0版本開始,Responsedective的發行版本以夏洛克·福爾摩斯(Sherlock Holmes)的佳能故事命名,按時間順序排列。如果我們達到60個發行版,沒有更多的故事會發生什麼?我們不知道,也許我們會在貓之類的東西之後開始命名它們。
該項目已根據MIT許可獲得許可。有關更多信息,請參見License.MD。