Adding jQuery to an iPhone project in XCode

Posted by Jerry | Posted in Development | Posted on 23-04-2010-05-2008

1

You might think that if you add the jquery js file to your project, you can build and reference it from html you load into a UIWebView. That’s what I thought too. Here are the steps you need to take.

1. Add the jquery file to resources. Drag the file in, right-click and choose to add an existing file. Whatever you are comfortable with.

Screen shot 2010-04-22 at 4-22-10 9.54.20 AM.png

2. The file will be automatically added as source code. In Xcode under Targets, expand the target you are working on and you’ll find two groups, “Copy Bundle Resources” and “Compile Resources”.

Screen shot 2010-04-22 at 4-22-10 9.55.00 AM.png

3. Drag the file from the “Compile Sources” group

Screen shot 2010-04-22 at 4-22-10 9.55.19 AM.png

to the “Copy Bundle Resources” group.

Screen shot 2010-04-22 at 4-22-10 9.55.42 AM.png

This tells Xcode to package the file up and include it with your app as a resource instead of compiling it.

4. Include the reference to jQuery in your HTML.

html = [NSString stringWithFormat:@"<html><head><script type='text/javascript' src='jquery-1.4.2.min.js'></script></head><body>%@</body></html>", body];

5. Set the baseURL using the path to your jQuery file.

[myWebView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”jquery-1.4.2.min.js” ofType:nil]]];

That’s it. You can now make jQuery calls in your code.

Comments posted (1)

Thank you very much. The only page under millions which helped :-)