UIWebView中的iOS loadHtmlString不适用于Javascript

Bag*_*yer 9 javascript uiwebview ios

我有一个iOS应用程序来加载一个html字符串,该字符串引用同一目录中的外部css文件和javascript文件.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

css文件工作正常,但似乎从未加载js文件.可能是什么原因?

顺便说一下,我使用loadHtmlString:BaseUrl方法来加载我的html字符串.

谢谢

ser*_*gio 7

这个问题有点陈旧,但由于没有得到解答,我想指出,如果你希望你的网页视图加载一个驻留在你的软件包中的javascript文件,那么这样做的方法是:

[webView loadHTMLString:html
               baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Run Code Online (Sandbox Code Playgroud)

如果您未指定baseURL,则无法加载javascript文件.


Kev*_*ers 5

默认情况下,xcode会将新的Javascript文件放在Compile Resources中而不是Bundle Resources中.单击您的项目,然后单击目标.在"Build Phases"下,您可以在"Compile Sources"下看到Javascript文件.将它们拖到"复制捆绑资源".