简体   繁体   English

UIWebView中的iOS loadHtmlString不适用于Javascript

[英]iOS loadHtmlString in UIWebView doesn't work with Javascript

I have a iOS app to load a html string which refer to external css file and javascript file in the same directory. 我有一个iOS应用程序来加载一个html字符串,该字符串引用同一目录中的外部css文件和javascript文件。

Here is my html file: 这是我的html文件:

<html>
<head>
    <meta name="viewport" content= user-scalable="no", width="device-width" />
    <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
    <script type="text/javascript" src="myjavascript.js"></script>
</head>

The css file works fine but it seems the js file never loaded. css文件工作正常,但似乎js文件从未加载。 What could be the reason? 可能是什么原因?

By the way I'm using loadHtmlString: BaseUrl method to load my html string. 顺便说一下,我使用loadHtmlString:BaseUrl方法来加载我的html字符串。

Thanks 谢谢

This question is a bit old, but since it has not been answered, I would like to point out that if you would like your web view to load a javascript file residing in your bundle, the way to do it is: 这个问题有点陈旧,但由于没有得到解答,我想指出,如果您希望您的Web视图加载驻留在您的包中的javascript文件,那么这样做的方法是:

[webView loadHTMLString:html
               baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

If you don't specify the baseURL there is no chance that the javascript file is loaded. 如果您未指定baseURL,则无法加载javascript文件。

By default, xcode will put new Javascript files in Compile Resources instead of Bundle Resources. 默认情况下,xcode会将新的Javascript文件放在Compile Resources中而不是Bundle Resources中。 Click on your project, then on your target. 单击您的项目,然后单击目标。 Under 'Build Phases' you may see the Javascript files under 'Compile Sources'. 在“Build Phases”下,您可以在“Compile Sources”下看到Javascript文件。 Drag them to down to 'Copy Bundle Resources'. 将它们拖到“复制捆绑资源”。

like the others have said 像其他人说的那样

  • use baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 使用baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

  • copy Bundle Resources and not in Compile Resources. 复制Bundle Resources而不是 Compile Resources。

thats it! 而已!

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

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