views:

835

answers:

2

Hello all,

I'm trying to use a UIWebView to display content, and jQuery to manipulate that content, but I can't get jQuery to load. Here's the code that I have setting up the UIWebView, setting the baseURL, and loading the content (an HTML file that's in the local bundle):

UIWebView *view = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];

NSData *htmlData = [[NSData alloc] initWithContentsOfURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]]];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
[view loadHTMLString:htmlString baseURL:baseURL];

The content loads just fine, and I've confirmed that the baseURL is correct by adding an image to the bundle and referencing it in the HTML, and it works. Using the same HTML in Safari on my Macbook jQuery is loading just fine, too, and working as expected.

I've tried both using the compressed jquery-1.3.2.min.js and the development jquery-1.3.2.js, and no dice for either.

Any ideas?

+1  A: 

Have you tried copying both the .html and .js files to the app's documents folder, and pointing the UIWebView to that local folder?

Alex Reynolds
+5  A: 

Gah! EBCAK!

Turns out when I copied the jquery javascript source into the project, it was copied into the target's "Compile Sources," which of course threw errors. I deleted it from "Compile Sources" but didn't add it back to "Copy Bundle Resources."

/me slaps forehead repeatedly.

John Biesnecker
I need to know what this means! How do you load the jQuery API into your project to manipulate page data?
JustinXXVII
By default, when you include jQuery's .js file in XCode, it puts it into your target's compiled folder, which is wrong. Go to your target in XCode, and drag the JQuery file from "Compile Sources" to "Copy Bundle Resources" and it'll load normally.
Don Jones