Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Mobile not working inside UIWebView

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.

like image 652
Juan González Avatar asked May 01 '12 04:05

Juan González


2 Answers

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

like image 67
Brad Cypert Avatar answered Oct 05 '22 05:10

Brad Cypert


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.

like image 24
Juan González Avatar answered Oct 05 '22 06:10

Juan González