简体   繁体   English

如何在Xcode中添加JavaScript代码,以便我可以在WebView中运行它

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

The ads which am integrating is from adbutler. 集成的广告来自adbutler。 The code for the ad is in javascript. 广告代码使用javascript。 i need to run the js code in UIWebview. 我需要在UIWebview中运行js代码。 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. 使您想要在网页中运行function()的代码。

From iOS WebView call stringByEvaluatingJavaScriptFromString: the function 从iOS WebView调用stringByEvaluatingJavaScriptFromString:该函数

JavaScript function: JavaScript函数:

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

iOS: 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. 您可能需要对要传递到javascript函数中的数据进行编码。
  2. you will have to \\" if you want javascript strings that you want to pass into the javascript function. 如果您想将JavaScript字符串传递给javascript函数,则必须\\“。
  3. You can get data back from stringByEvaluatingJavaScriptFromString: but it will be a string, so be careful there. 您可以从stringByEvaluatingJavaScriptFromString取回数据:但这将是一个字符串,因此请当心。 I never had that with my app, but it can happen. 我的应用程序从未有过这种功能,但是有可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何优化我的代码,使其可以有效运行? - How can I optimize my code so, it can run effectively? 在 Webview 中运行 javascript 代码 - Run javascript code in Webview 如何减少此代码的javascript代码,使其不那么重复? - How can I reduce this code javascript code so it's not as repetitve? 如何重写代码,以便我可以在Acrobat上运行它? - How to rewrite a code, so i can run it on Acrobat? 我该如何重写此javascript代码,以便它将引用每个optionbox? - How can I rewrite this javascript code, so that it will refer to every optionbox? 如何加密 JavaScript 代码使其不可解密? - How can I encrypt JavaScript code so that it's not decryptable? 如何在Xcode中将js代码添加到我的iPhone应用程序 - how can i add a js code to my iphone app in xcode 如何在服务器端运行 JavaScript 代码 Java 代码? - How can I run JavaScript code at server side Java code? 我可以在没有WebView的情况下运行Javascript,还是可以在没有Activity上下文的情况下运行WebView,例如在服务中? - Can I run Javascript without a WebView, or can a WebView run without an Activity context, such as in a Service? 如何使用Java代码中的Javascript接口将数据传递到WebView中的Javascript? - How can I pass data to Javascript in a WebView using a Javascript interface in Java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM