How to detect authentication request from UIWebView?

I have seen many questions that ask (and answer) how to provide credentials when you know in advance that the request requires them.

When I load a site that requests an authentication call (responds with HTTP 401), my UIWebViewDelegate receives only the following callbacks: webView:shouldStartLoadWithRequest:navigationType: and sometimes webViewDidStartLoad: ( webViewDidStartLoad: seems to be called if I go directly to the site, and doesn't seem to be called if I am redirected there).

Overriding -[NSObject respondsToSelector:] , the UIWebViewDelegate receives some calls to uiWebView:resource:canAuthenticateAgainstProtectionSpace:forDataSource: and uiWebView:resource:didReceiveAuthenticationChallenge:fromDataSource: which can be useful, of course, but of course.

I tested several different applications with built-in UIWebView s. So far, only Chrome has handled these authentication tasks properly. I even set up my own server and confirmed that Chrome only POST once. Twitter and Tweetbot just show the download screen forever, like mine.

Is there any other (possibly very hacky) way to detect these authentication problems besides using the second speculative NSURLConnection ? If I make a POST request that will be double- POST all that is bad.

I added a radar to request improvements for the UIWebView for this . Please duplicate it.

+6
source share
2 answers

Apple now has sample code that solves this problem . It also looks like the most comprehensive documentation on how to properly implement NSURLProtocol.

+6
source

You can load all requests into your own NSURLConnection , and then load the data into a web view as a string. This will allow you to intercept the connection in all its glory and still get the native rendering of the UIWebView . I'm not sure if this will work well with streaming data to a web view as it arrives, but hopefully we are talking about too much data here.

+3
source

Source: https://habr.com/ru/post/943714/


All Articles