简体   繁体   中英

How to add a JavaScript code in xcode so that i can run it in webview

The ads which am integrating is from adbutler. The code for the ad is in javascript. i need to run the js code in UIWebview. Any suggestions.

The sample code is :

<script type="text/javascript">
var rnd = window.rnd || Math.floor(Math.random()*10e6);
var pid156452 = window.pid156452 || rnd;
var plc156452 = window.plc156452 || 0;
var abkw = window.abkw || '';
var absrc = 'http://ab152067.adbutler-photon.com/adserve/;ID=152067;size=320x100;setID="some number";type=iframe;kw='+abkw+';pid='+pid156452+';place='+(plc156452++)+';rnd='+rnd+'';
document.write('<ifr'+'ame src="'+absrc+'" width="320" height="100" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></ifr'+'ame>');
</script>

make the code you want to run a function() in the web page.

From iOS WebView call stringByEvaluatingJavaScriptFromString: the function

JavaScript function:

function adCrapFunction()
{
   alert('Hey there buy something.');
}

iOS:

[self.webview stringByEvaluatingJavaScriptFromString:@"adCrapFunction();"];

Some things that will get you:

  1. you might have to encode data that you want to pass into the javascript function.
  2. you will have to \\" if you want javascript strings that you want to pass into the javascript function.
  3. You can get data back from stringByEvaluatingJavaScriptFromString: but it will be a string, so be careful there. I never had that with my app, but it can happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM