27

I have the following innocent-looking code (index.html), but it doesn't load the code.js file. It works well if I copy/paste the contents of the file in the HTML page. Both index.html and code.js files are in the same directory.

index.html:

<html>
<head>
<script type="text/javascript" src="code.js"></script>
</head> 
<body onload="drawImage()">
  <div><canvas id="canvas" width="320" height="480"></canvas></div> 
</body>
</html>

ViewController.m:

- (void)viewDidLoad {
  [super viewDidLoad];
// load the first webpage
[homePageWebView loadRequest:[NSURLRequest requestWithURL:[NSURLfileURLWithPath:
  [[NSBundle mainBundle] pathForResource:@"index"ofType:@"html"] isDirectory:NO]]];
}

4 Answers 4

59
+50

I have found that Xcode thinks that the js file needs to be compiled.

Open the project in Xcode. Go to "Targets" and look in "Compile Sources". If you see your js file in that folder, move it to the "Copy Bundle Resources" folder and delete it from the "Compile Sources" folder.

Delete the app from your iPhone if it is installed already for testing. Go to the Build menu in Xcode and Clean all Targets. Then recompile.

Check now to see if your HTML file actually sees your js file now.

Linda

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

12 Comments

And where does one find "Targets" in Xcode? I'm running the latest version doing ipad dev and there is no such named screen....
oh wait i found it. It's a bucket on the left hand side
ehm... if you found your answer. Please close the question :)
I cant get it to work with the js files located in a subfolder. Only works when the js files is in the same folder as the html file :/
To be precise: Click on your project in the project navigator on the left. The main pane should now have a sidepane on the left. This sidepane should have "PROJECT" and "TARGETS". Click on you project under the "TARGETS" heading. The pane to the right should now have a bunch of tab headings going "Summary", "Info"... etc. Click on "Build Phases". This should then show a bunch of subsections. Open up the sections "Compile Sources" and "Copy Bundle Resources." Then, click and drag ALL the javascript files from the "Compile Sources" section to the "Copy Bundle Resources" section.
|
7

You need to read the html file into a string and set the baseURL when loading so relative paths can be understood.

NSError* error;
NSString* html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] encoding:NSASCIIStringEncoding error:&error];

[webview loadHTMLString:html baseURL:[self getBaseURL]];

- (NSURL*) getBaseURL {
    NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSRange r = [path rangeOfString:@"/" options:NSBackwardsSearch];
    path = [path substringToIndex:r.location];

    path = [path stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    path = [NSString stringWithFormat:@"file:/%@//",path];
    return  [NSURL URLWithString:path];
}

Something like that should work for you.

8 Comments

I think if you set base in the header of the HTML document, it will work too, and will not require any additional code.
How do you set the base header for a local file. It tried by putting <base href="./"> and <base href="/"> in the <head>, but still doesnt work. Can you give me an example please.
How could you know what the base URL of the file is going to be ahead of time? It would change depending on where it was deployed.
oh so sorry, it wasnt clear above, all the files are in the resource bundle, and hence local to the app.
This should not be needed. Loading a local file:// url will work fine.
|
6

To supplement Linda's Answer here is where you find the files:

enter image description here

Comments

5

I also faced the same problem: I overcame it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below:

In the figure below, one is for adding HTML, Javascript and CSS like files and the second is for general XCode files.

for HTML, Javascript and CSS files

For general XCode files

1 Comment

Simple and effective solution :)

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.