App Transport Security breaks Web View

跟風遠走 提交于 2019-12-12 11:35:38

问题


My app which works great under iOS8 no longer runs under iOS9. The problem is that despite having the following in my .plist file:

<key>NSAppTransportSecurity</key>
<dict>
   <!--Include to allow all connections (DANGER)-->
   <key>NSAllowsArbitraryLoads</key>
   <true/>
</dict>

The following code:

NSURL *targetURL = [NSURL URLWithString:_caseStudyListTitleURL];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_myWebView loadRequest:request];

results in the error:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

This of course leads to an empty webView being displayed.

All other NSURLSession Code in the app is functioning correctly.

I am running XCode 7 Beta 3 and iOS 9 on my test iPad.

Any ideas on this would be greatly appreciated!


回答1:


Try this solution:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>



回答2:


It is happening because you're using a self-signed certificate, it's necessary to add some additional code. Because by default iOS will reject all untrusted https connections.

Restricting untrusted connections is very good default behaviour and any disabling of this is highly risky.

Use this method to bypass authentication challenge:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

//code to cancel authentication challenge

}

In case you don't know how to use it to cancel authentication challenge then you can find some great help from this article regarding loading HTTPS url in WebView.

Also have a look at this answer as well.



来源:https://stackoverflow.com/questions/31305061/app-transport-security-breaks-web-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!