NSAppTransportSecurity UIWebView issue

2019-08-09 06:01发布

问题:

I have these lines in my info.plist file:

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

So it works perfect with my API calls.

In my view controller I have UIWebView that tries to display web content:

NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webSiteUrlSting] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];

[self.theWebView loadRequest:request];

on iOS7 and iOS8 it works great, but on iOS9 it shows an error after invocation UIWebView delegate - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

The delegate method contain error instance that looks like this:

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7fbaa253ba20 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSErrorFailingURLStringKey=requestedwebsite.com, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLKey=requestedwebsite.com}}, NSErrorFailingURLStringKey=requestedwebsite.com, NSErrorFailingURLKey=requestedwebsite.com, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}

My question is how to keep my API requests work and make UIWebView work as well.

回答1:

Add this exception in your NSAppTransportSecurity dict:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>