Pawan Joshi
Pawan Joshi

Reputation: 1579

how to include jquery library in Xcode project?

I have a UIWebView which loads local .html files from resource bundle. everything is working fine except that the Javascript code written inside .html file is not working at all. I don't have .js file to show popup view in .html file. instead of it the JS code is written inside .html file.

here are some screen shots of my xcodeproject :-

enter image description here

enter image description here

by looking around over google all i could gather was that, since i am not using network connection inside my app at all,(i am loading everything locally ) the src for jQuery Library isn't being downloaded.

so is there something which can replace following line in .html file

" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">"

Or Am i going with the wrong approach ??

EDIT

if this helps :-

i have also tried opening those .html files in safari inside my MAC.. . . JS code didn't work there too.

well . . .

am i trying to accomplish something impossible here ?... :D

Upvotes: 0

Views: 3408

Answers (2)

Merlin
Merlin

Reputation: 4927

You need to download jquery locally and point to your local jquery file into <script> tag

e.g : local jquery path

/my_local_folder/js/jquery/jquery.min.js

Changing

<script src="ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

Width the local jquery file

<script src="my_local_folder/js/jquery/jquery.min.js">

Upvotes: 2

Michael Giba
Michael Giba

Reputation: 118

Taken from http://www.altinkonline.nl/tutorials/xcode/uiwebview/load-jquery-in-uiwebview/:

after setting the delegate of the web view to some object you can modify the

- (void)webViewDidFinishLoad:(UIWebView *)webView {

method body as follows

if loading from CDN you would do something like:

NSString *jqueryCDN = @"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
NSData *jquery = [NSData dataWithContentsOfURL:[NSURL URLWithString:jqueryCDN]];
NSString *jqueryString = [[NSMutableString alloc] initWithData:jquery encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jqueryString];

but if you save a minimized JQuery file locally within the bundle you can do:

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

Upvotes: 0

Related Questions