ResponseDetective 是一个非侵入式框架,用于拦截你的应用程序和服务器之间的传出请求和传入的反应,可用于调试。
使用:
Step 1: Register interceptors
// request InterceptingProtocol.registerRequestInterceptor(BaseInterceptor()) InterceptingProtocol.registerRequestInterceptor(JSONInterceptor()) // response InterceptingProtocol.registerResponseInterceptor(BaseInterceptor()) InterceptingProtocol.registerResponseInterceptor(JSONInterceptor()) InterceptingProtocol.registerResponseInterceptor(HTMLInterceptor()) // error InterceptingProtocol.registerErrorInterceptor(BaseInterceptor())
Step 2: Register the protocol
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.protocolClasses = [InterceptingProtocol.self] let session = NSURLSession(configuration: configuration) let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.protocolClasses = [InterceptingProtocol.self] let manager = Alamofire.Manager(configuration: configuration)
Step 3: Profit
let request = NSURLRequest(URL: NSURL(string: "http://httpbin.org/get")!) let task = session.dataTaskWithRequest(request) task.resume()