0

so I am writing a radio station app that dynamically loads ads. The original solution was to go to https://cdn.azureradio.com/invocation/clearwater/iOS/the_beat.html in a WebView and then I used Swift Soup and souped the webview for the variables I needed. However, I recently found out that Web Views do not load requests in the background, IE: if the device is locked.

This obviously poses an issue. With that said, I decided on making an HTTP request. I called the url and got a Javascript string. I need to know of a way to run the javascript code and put the html that it generates into a variable so that I can soup the variable. Currently, the Javascript code that I get is

<!-- Azure Spot Server Javascript Tag - Generated with Revive Adserver v5.0.5 -->
<script type='text/javascript'><!--//<![CDATA[
   var m3_u = (location.protocol=='https:'?'https://azureradio.com/ads/www/delivery/ajs.php':'http://azureradio.com/ads/www/delivery/ajs.php');
   var m3_r = Math.floor(Math.random()*99999999999);
   if (!document.MAX_used) document.MAX_used = ',';
   document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
   document.write ("?zoneid=7&amp;target=_top");
   document.write ('&amp;cb=' + m3_r);
   if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
   document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
   document.write ("&amp;loc=" + escape(window.location));
   if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
   if (document.context) document.write ("&context=" + escape(document.context));
   if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
   document.write ("'><\/scr"+"ipt>");
//]]>--></script>

I have no idea how to run this code in swift to generate a URL without using webView.evaluateJavaScript("document.documentElement.outerHTML.toString()") Thank you very much.

4
  • Does this answer your question? Run javascript without UIWebView possible? Commented May 2, 2020 at 20:32
  • That did not work. Commented May 2, 2020 at 21:10
  • which of 3 methods described there did you try? Commented May 2, 2020 at 21:36
  • I tried all of them. Commented May 3, 2020 at 2:27

1 Answer 1

3

You can use JavaScriptCore, which is a way to directly access WebKit's JavaScript engine without actually using an instance of a web view.

import JavaScriptCore

let context = JSContext()!
let value = context.evaluateScript(yourJavaScriptString)
print(value)

The value that is returned is the value of the last element that was evaluated.

Sign up to request clarification or add additional context in comments.

4 Comments

So I used let context = JSContext()! let value = context.evaluateScript(contents) print(value!) and the issue is it prints undefined, then says "Could not cast value of type 'JSValue' (0x1dec940c8) to 'NSString' (0x1debc2018)."
@BrianBecker Try to use value.toString() developer.apple.com/documentation/javascriptcore/jsvalue/…
It is still undefined.
I decided to put the sleep(4) to allow time for the Javascript to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.