Alamofire: Can't use NSURLProtocol intercepts requests

Created on 13 May 2016  路  2Comments  路  Source: Alamofire/Alamofire

As far as I know, NSURLProtocl could intercepts all reqeuests sent by URL Loading System. But after testing Alamofire (as well as AFN), My custom NSURLProtocol couldn't intercepts those requests sent by it, whici means the protocol's "canInitWithRequest" won't be consulted, but for those sent by NSURLSession, this method could be invoked.

anyone could help me ?
thx

question url protocol

Most helpful comment

@cnoon锛宨t turns out that if you use a custom NSURLSessionConfiguration (rather than sharedSession()), you are gonna have to do the following to make your NSURLProtocol make difference:

        let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

         if sessionConfiguration.protocolClasses != nil {
                sessionConfiguration.protocolClasses!.insert(KLXFakeDataProtocol.self, atIndex: 0)
         }
        // use custom sessionConfiguration to initialize your NSURLSession / Alamofire.Manager

this part is neither documented in the NSURLProtocol References nor majority of tutorials on the internet.
Hope may helop someone.

All 2 comments

These tests demonstrate how you could use an NSURLProtocol with Alamofire if you wanted @Yannmm.

Best of luck! 馃嵒

@cnoon锛宨t turns out that if you use a custom NSURLSessionConfiguration (rather than sharedSession()), you are gonna have to do the following to make your NSURLProtocol make difference:

        let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

         if sessionConfiguration.protocolClasses != nil {
                sessionConfiguration.protocolClasses!.insert(KLXFakeDataProtocol.self, atIndex: 0)
         }
        // use custom sessionConfiguration to initialize your NSURLSession / Alamofire.Manager

this part is neither documented in the NSURLProtocol References nor majority of tutorials on the internet.
Hope may helop someone.

Was this page helpful?
0 / 5 - 0 ratings