2

I am just loading a local html file in my webview. This html file referances .js files also stored locally. The webview loads the html file properly but does not respond to the javascript functions defined in .js files. This is the way how I am loading my local html file.

[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

Note that I have already removed the warning

warning: no rule to process file '$(PROJECT_DIR)/html/orientation.js' of type sourcecode.javascript for architecture i386

But still it doesn't work out. Please help. Thanks Laura

3 Answers 3

2

Since .js file is a source code file Xcode does not know whether to compile it or include in bundle resources unchanged. If you link external .js files in your html code here's how to force them to be included in resources so they can be loaded from html.

  1. Xcode < 4.0: Look inside left pane, expand Targets and drag .js files into Copy Bundle Resources. Build.
  2. Xcode >= 4.0: Select your project in the left pane. Choose your Target. Expand Copy Bundle Resources and drag .js files to the list that will occur. Build.

To check if .js files were included in the bundle go to project's build folder, right-click on it and choose View Contents. If everything went ok you should see them there.

Sign up to request clarification or add additional context in comments.

Comments

0

This drove me nuts too. After trying out a few things, this is what I did to get it to work:

NSString *root = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"Player.js" ofType:@"txt"];
NSData *htmlData1 = [NSData dataWithContentsOfFile:filePath1];

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Player.js", root] 
                                        contents:htmlData1
                                      attributes:nil];

For whatever reason, there seems to be a problem with .JS extensions. So what you do is use a different type of extension (in my case, I used .js.txt) and save that to a location that your .html can find it.

Hope this helps! And I'd love to hear a more valid fix or hear why .js files don't work.

5 Comments

appreciate ur help. But actually I need to load the html file in my webview not .js file. The html file is referancing the .js file. So I cannot directly load the .js file.
The code I wrote doesn't load the .js file, it saves it in such a way that it's accessible by the UIWebView that gets its source for the .html file.
Oh ya i just clearly saw it. actualy was so much frustated by doing research for this that lost my energy. Sorry :)
I manually saved the .js file with .js.txt extention in my resources folder and then tried ur code but this din't work out.
I think this is due to xcode not copying some of your files attached to the project to your app bundle. I have experienced this issue for .js,.scpt and .prefPane files. Up to now I have been copying such files manually after building the app (to the app bundle -> contents -> Resources). I'm assuming that you know how to open the built app bundle by right clicking it in the finder.
0

In XCode 4 it's not enough to make sure the .js-File is included in the "Copy Bundle Ressources" list, it also must NOT be included in the "Compile Sources" list, which it will be by default.

The way I achieved it was by manually setting the file type in XCode to "plain text"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.