There's a load of questions which ask this: Can I get UIWebView
to view a self signed HTTPS website?
And the answers always involve either:
- Use the private api call for
NSURLRequest
:allowsAnyHTTPSCertificateForHost
- Use
NSURLConnection
instead and the delegatecanAuthenticateAgainstProtectionSpace
etc
For me, these won't do.
(1) – means I can't submit to the app store successfully.
(2) – using NSURLConnection means the CSS, images and other things that have to be fetched from the server after receiving the initial HTML page do not load.
Does anyone know how to use UIWebView to view a self-signed https webpage please, which does not involve the two methods above?
Or – If using NSURLConnection
can in fact be used to render a webpage complete with CSS, images and everything else – that would be great!
Cheers,
Stretch.
Best Answer
Finally I got it!
What you can do is this:
Initiate your request using
UIWebView
as normal. Then - inwebView:shouldStartLoadWithRequest
- we reply NO, and instead start an NSURLConnection with the same request.Using
NSURLConnection
, you can communicate with a self-signed server, as we have the ability to control the authentication through the extra delegate methods which are not available to aUIWebView
. So usingconnection:didReceiveAuthenticationChallenge
we can authenticate against the self signed server.Then, in
connection:didReceiveData
, we cancel theNSURLConnection
request, and start the same request again usingUIWebView
- which will work now, because we've already got through the server authentication :)Here are the relevant code snippets below.
Note: Instance variables you will see are of the following type:
UIWebView *_web
NSURLConnection *_urlConnection
NSURLRequest *_request
(I use an instance var for
_request
as in my case it's a POST with lots of login details, but you could change to use the request passed in as arguments to the methods if you needed.)I hope this helps others with the same issue I was having!