Cordova iOS with Failed to load webpage with error: Could not connect to the server.

This problem is caused by the transport security policy changed on iOS9. It affects the UIWebView of iOS9. New WKWebView is not affected by this problem.


Check if the cordova-ios version is larger than 4.0:
cordova platform version ios
If cordova-ios is larher than 4.0, please use Cordova WKWebView Engine with http server (localhost)support: 
https://github.com/apache/cordova-plugins/tree/master/wkwebview-engine-localhost

cordova plugin add https://github.com/apache/cordova-plugins.git#master:wkwebview-engine-localhost
Add the following tag in config.xml if using WKWebView:

<feature name="CDVWKWebViewEngine">
  <param name="ios-package" value="CDVWKWebViewEngine" /> 
</feature>
<preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />
You must specify the wkwebview server port on config.xml for ios using indexdb or localstorage (port e.g. 12344 ) rather than the randomize port :

<preference name="AlternateContentSrc" value="http://localhost:12344" />
instead of

Cordova WKWebView Polyfill Plugin

https://github.com/Telerik-Verified-Plugins/WKWebView
cordova plugin add cordova-plugin-wkwebview --variable WKWEBVIEW_SERVER_PORT=12344

OR

To solve this problem you may use the new WKWebView with Cordova or you may add the follow key in the info.list:


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

or


<key>NSExceptionDomains</key>
  <dict>
    <key>localhost</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <false/>
      <key>NSExceptionAllowsInsecureHTTPLoads</key>
      <false/>
      <key>NSExceptionRequiresForwardSecrecy</key>
      <true/>
      <key>NSExceptionMinimumTLSVersion</key>
      <string>TLSv1.2</string>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <false/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <true/>
      <key>NSThirdPartyExceptionMinimumTLSVersion</key>
      <string>TLSv1.2</string>
      <key>NSRequiresCertificateTransparency</key>
      <false/>
    </dict>
  </dict>
    
  


or just put this in the config.xml:

    
        
<platform name="ios">
    <config-file platform="ios" target="*-Info.plist" parent="NSAppTransportSecurity">
        <dict>
            <key>NSAllowsArbitraryLoads</key><true/>
        </dict>
    </config-file>
</platform>
        
    

留言

這個網誌中的熱門文章

Disable ionic's sidemenu content drag to toggle menu

Multiple writable mappings exist for the field. Only one may be defined as writable, all others must be specified read-only.

java.lang.NoClassDefFoundError: org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl$Parser