Allow unverified ssl certificate in UIWebview

后端 未结 7 1957
北恋
北恋 2020-12-02 07:05

I\'m embedding a website in a UIWebView. During development I have it pointed at localhost. The problem is that whenever it hits a \"https://\" url it doesn\'t load. When I

7条回答
  •  日久生厌
    2020-12-02 07:40

    There's a way to do this legally (by App Store laws at least). When you use the NSURLConnection there are 2 methods that can be used to allow self-signed SSL certificates to be used:

    How to use NSURLConnection to connect with SSL for an untrusted cert?

    If you implement UIWebViewDelegate use the

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
    

    Return NO to this so that the WebView doesn't load on its own. Then construct an NSURLConnection (which can be used with unsigned certificates via the above link).

    Of course the usual SSL recommendations apply here:
    -Don't use an unsigned cert on production servers!
    -Always surface a warning letting your user decide whether to accept the cert or not.

提交回复
热议问题