JQuery Mobile not working inside UIWebView
Asked Answered
M

2

12

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.

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.

I'm not using 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).

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:

enter image description here

enter image description here

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

enter image description here

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

enter image description here

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

If I test Javascript with an alert like this:

enter image description here

It will show the alert proving Javascript is working:

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:

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.

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.

Miscellaneous answered 1/5, 2012 at 4:16 Comment(0)
M
10

Nevermind guys. I finally got it.

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

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

<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:

<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.

Miscellaneous answered 1/5, 2012 at 12:18 Comment(5)
I'm having the same problem, but with a accordion test. Can you help me?Frigidarium
Didn't the information posted here help you already? If not, what's the specific problem you're having?Genitalia
Unless you check "Create folder references for added folders" when you add the files to your project.Prestige
One of one those moments.. Gah. Spending hours to frustration figuring out it was a to obvious solutionStopover
What if you did want it to work with folders? How do you do this? Is the only way Miles comment answer?Julian
S
4

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.

 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 :)

Skewness answered 7/8, 2012 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.