javascript – WKWebView not executing script configured on iOS localhost


I have a WKWebView set up to load a webpage where part of it’s contents is the following

<p>
   <script language="javascript">var jdownloader=false;</script>
   <script language="javascript" src="http://127.0.0.1:9666/jdcheck.js"></script>
   <script language="javascript">
   if(jdownloader){
      document.write("(JDownloader is currently running)");
   }else{
      document.write("(JDownloader is NOT running)");
   }
</p>

(link)

When loading the page this should run a script located at ‘http://127.0.0.1:9666/jdcheck.js’. I have GCDWebServer configured to run on localhost, returning jdownloader=true;. This is then supposed to print ‘JDownloader is currently running’ on the webpage.

I can’t work out why the WKWebView does not call this script. I know GCDWebServer is configured correctly, as I can call /jdcheck.js from another computer on my local network (using my iPhone’s IP address), and I get the expected response.

Secondly, when selecting a button on the webpage to call localhost, it works as expected as well:

<form action="http://127.0.0.1:9666/flash/addcrypted2" target="hidden" method="POST">
   .....
</form>

When I load the html as a string into the WKWebView, it also works as expected. It’s just when loading it directly from the url.

So it just seems as though WKWebView isn’t even trying to run the script when the page loads (I can see from some logging that /jdcheck.js is not called). I’m not quite sure how to debug this, or what to try next?

NSAppTransportSecurity looks like this:

<key>NSAppTransportSecurity</key>
    <dict>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
        <key>127.0.0.1</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img