2

I want to add a custom header field to every request made by my UIWebView. First I tried to do this in the "shouldStartLoadWithRequest" method in my controller, but this only works for the first request made - all subsequent requests are not handled by this method.

After some search, I found this tutorial (http://eng.42go.com/customizing-uiwebview-requests-with-nsurlprotocol/). So I implemented my own NSURLProtocol subclass, and everthing is working fine, except my login page - there is some problem with authentication, and I could not solve this. I saw that there exists some methods like "canAuthenticateAgainsProtectionSpace" and "didReceiveAuthenticationChallenge", and I probably need to implement these in order to my login page to work. However, this seems a lot of additional work, since in the first place I just wanted to add a field to every request.

So my doubts are:

  • Is there any other easy way to do this?
  • If not, should I implement all these authentication methods?
  • Is there a way to my custom NSURLProtocol subclass to extends from something like NSURLHTTPProtocol (therefore I would only have minimal work) instead of the NSURLProtocol itself?

Thanks!

guilhermekrz
  • 68
  • 11
  • 1
    `webView:shouldStartLoadWithRequest:navigationType:` delegate method should be called for every request, even for the images in your webpage. Maybe you are changing the `delegate` after the first request? – ismailgulek Apr 01 '14 at 13:44
  • 1
    Probably worth checking the delegates are actually firing - I've had endless problems with this in the past. – GuybrushThreepwood Apr 01 '14 at 13:51
  • I double checked my code, and I think I am not changing the delegate after the first request. In the webView:shouldStartLoadWithRequest:navigationType [description](https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIWebViewDelegate/webView:shouldStartLoadWithRequest:navigationType:) it is stated "Sent before a web view begins loading a frame.". Doesn´t this mean that it is only called for the first request? – guilhermekrz Apr 01 '14 at 18:02
  • 1
    Yes that means it is only called in the first request. – Arun Poudel Oct 17 '14 at 14:46

0 Answers0