1

I am developing a framework where i wanna apply interceptor for all network calls,

i have managed to add interceptor at single object level, n figuring out how to add it on app level without adding my guard class each time.

code for adding interceptor at object level

func getURLSessionConfiguration() -> URLSessionConfiguration
    {
    let configuration = URLSessionConfiguration.default
            configuration.protocolClasses?.append(MyURLProtocol.self)
    return configuration
}


let defaultSession = URLSession(configuration: getURLSessionConfiguration())
inni
  • 464
  • 3
  • 16

1 Answers1

0

The only way I know of is to swizzle the init method for NSURLSessionConfiguration and wrap the method's existing implementation with code that pre-injects your NSURLProtocol into the set of protocols.

If you do swizzle, be sure to do so by writing a C function that calls the original IMP directly, passing it the original selector, not by renaming the method.

For details, see What are the Dangers of Method Swizzling in Objective C?.

dgatwood
  • 10,129
  • 1
  • 28
  • 49