xcode JQuery Mobile 无法在 UIWebView 中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10393490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:09:05  来源:igfitidea点击:

JQuery Mobile not working inside UIWebView

iosxcodejquery-mobileuiwebviewfusioncharts

提问by Juan González

I'm coming here after many many hours of looking for a solution and trying different approaches to fix this issue I'm having with JQuery Mobile and my iPad app.

经过许多小时的寻找解决方案并尝试不同的方法来解决我在 JQuery Mobile 和 iPad 应用程序中遇到的这个问题,我来到这里。

What I'm trying to do is a reporting app with Fusion Charts. I have successfully rendered the charts within web views (UIWebView) using different methods, and following different examples, but what I'd like to do now is accomplishing the same task using the JQuery Mobile framework.

我想要做的是一个带有 Fusion Charts 的报告应用程序。我已经使用不同的方法并遵循不同的示例成功地在 Web 视图 (UIWebView) 中呈现图表,但我现在想做的是使用 JQuery Mobile 框架完成相同的任务。

I'm notusing phonegap and I've followed the steps to the letter regarding JQuery Mobile + Xcode integration (I think). The charts load without a problem on my desktop and mobile browsers (iPad , iPhone 4, and iPod Touch 2G), but there's no way they load inside my UIWebView (even though I'm using the same exact code).

没有使用 phonegap 并且我已经按照有关 JQuery Mobile + Xcode 集成的步骤操作(我认为)。图表在我的桌面和移动浏览器(iPad、iPhone 4 和 iPod Touch 2G)上加载没有问题,但它们无法加载到我的 UIWebView 中(即使我使用的是完全相同的代码)。

This is what I'm doing:

这就是我正在做的:

1) Adding required files (JS, CSS, and HTML) to the project to use them locally. After adding them I move everything with .js extension from "Compile Sources" to "Copy Bundle Resources". It looks like this:

1) 将需要的文件(JS、CSS 和 HTML)添加到项目中以在本地使用它们。添加它们后,我将带有 .js 扩展名的所有内容从“编译源”移动到“复制捆绑资源”。它看起来像这样:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

2) Load my HTML test file (page_A1.html) into the web view:

2) 将我的 HTML 测试文件 (page_A1.html) 加载到 Web 视图中:

enter image description here

在此处输入图片说明

3) Check that everything is set up on the HTML file with correct paths and latest version of frameworks:

3) 检查 HTML 文件中的所有内容是否都设置正确,并且具有正确的路径和最新版本的框架:

enter image description here

在此处输入图片说明

4) Build and Run only to get a blank web view.

4) 构建并运行只是为了得到一个空白的 web 视图。

If I test Javascript with an alert like this:

如果我用这样的警报测试 Javascript:

enter image description here

在此处输入图片说明

It will show the alert proving Javascript is working:

它将显示证明 Javascript 正在工作的警报:

enter image description here

在此处输入图片说明

And if I put some text on the container where the chart is supposed to be displayed:

如果我在应该显示图表的容器上放一些文本:

enter image description here

在此处输入图片说明

I'll get the text inside the web view:

我将在 web 视图中获取文本:

enter image description here

在此处输入图片说明

I have also tested with remote framework links instead of the local ones and older version of the libraries with no luck. Nothing different happens:

我还使用远程框架链接而不是本地链接和旧版本的库进行了测试,但没有运气。没有什么不同的发生:

enter image description here

在此处输入图片说明

It seems obvius to me that

对我来说似乎很明显

$(document).ready(function(){

is not being noticed or invoked by xcode.

没有被 xcode 注意到或调用。

This is what I'd like to accomplish:

这是我想要完成的:

enter image description here

在此处输入图片说明

I don't know what else to test. If you have any idea I'd be more than grateful.

我不知道还有什么要测试的。如果您有任何想法,我将不胜感激。

采纳答案by Juan González

Nevermind guys. I finally got it.

没关系伙计们。我终于明白了。

Some old habits from the web development days played me here.

Web 开发时代的一些旧习惯让我在这里。

It tuns out you don't have to specify the inner directory structure for your CSS and JS files so this:

事实证明,您不必为 CSS 和 JS 文件指定内部目录结构,因此:

<link rel="stylesheet" href="css/jquery.mobile-1.1.0.min.css" />

<script src="js/jquery-1.7.2.min.js"></script>

<script src="js/jquery.mobile-1.1.0.min.js"></script>

shoudl really be like this in Xcode:

在 Xcode 中真的应该是这样的:

<link rel="stylesheet" href="jquery.mobile-1.1.0.min.css" />

<script src="jquery-1.7.2.min.js"></script>

<script src="jquery.mobile-1.1.0.min.js"></script>

Unbelievable how much time it took me to find that out.

难以置信我花了多少时间才发现。

Now it's working.

现在它正在工作。

回答by Agent 404

The correct way to do this is to load the JS file as in a String and invoke the UIWebView's [stringByEvaluatingJavascriptFromString:@"Javascript here..."] method.

执行此操作的正确方法是将 JS 文件作为字符串加载并调用 UIWebView 的 [stringByEvaluatingJavascriptFromString:@"Javascript here..."] 方法。

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

[webView stringByEvaluatingJavaScriptFromString:jsString];

Although this isn't much different then hardcoding them into the html files, it does modularize it even more. Happy Coding :)

尽管这与将它们硬编码到 html 文件中没有太大不同,但它确实更加模块化。快乐编码:)