5

I have following structure of my www folder in Documents directory in ios

Documents
   --www
      --index.html
      --js
          --script.js
      --css
          --style.css

My index.html file references script and style files as below:

<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/script.js"></script>

Please note, the files and directory structure are created after app launch, from a remote-downloaded and extracted zip file. So this is not an issue caused by Xcode folder groups being confused with the app bundle's Documents subfolders.

It works fine when run in a browser but when loading index.html in UIWebview programmatically, script and style are not working. Meanwhile the index file itself loads perfectly.

When I give full path of script and css in index.html file it working fine for me.

Why relative path not working?

Thanks in advance.

1
  • Without seeing it, I believe your code for loading the index.html file programmatically doesn't have a baseURL parameter. I posted a full answer with more details. Commented Dec 13, 2013 at 8:11

3 Answers 3

4
+50

Apparently the document base is not the same as the app's Documents directory when loading the HTML file programatically.

Check out the BASE element in HTML, it goes within the <head> element:

http://www.w3.org/wiki/HTML/Elements/base

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>This is an example for the <base> element</title>
        <base href="http://www.example.com/news/index.html">
    </head>
    <body>
        <p>Visit the <a href="archives.html">archives</a>.</p>
    </body>
</html>

To fix that, you'll have supply a Document base url manually. You haven't posted the code how you're programmatically loading the index.html file, so I will assume you're using the following UIWebView method:

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

So now try out this code, see if it solves your problem:

// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

// Add www/index.html to the basepath
NSString *fullFilepath = [basePath stringByAppendingPathComponent:@"www/index.html"];// This must be filled in with your code

// Load the file, assuming it exists
NSError *err;
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];

// Load the html string into the web view with the base url
[self.webview loadHTMLString:html baseURL:[NSURL URLWithString:fullFilepath]];

If that doesn't work, try updating the code of the HTML file that you get from the .zip file. Something like:

// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

// Add www/index.html to the basepath
NSError *err;
NSString *fullFilepath = [basePath stringByAppendingPathComponent:@"www/index.html"];
// Load the file, assuming it exists
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];
// Check to ensure base element doesn't already exist
if ([html rangeOfString:@"<base"].location == NSNotFound) {
    // HTML doesn't contain a base url tag
    // Replace head with head and base tag
    NSString *headreplacement = [NSString stringWithFormat:@"<head><base href=\"%@\">", fullFilepath];
    html = [html stringByReplacingOccurrencesOfString:@"<head>" withString:headreplacement];
    [html writeToFile:fullFilepath atomically:YES encoding:NSUTF8StringEncoding error:&err];
}
// Now the file has a base url tag
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for giving suggestion. This is really helpful. i update my correct answer.
3

May be your JS And CSS File are not compiled by xcode. Open your project with xcode and GO to 'Targets' and check in the 'Compile Sources' section . If your js file is present in that folder, move it to the 'Copy Bundle Resource' and delete is from "Compile Sources" folders.

After that delete app from your device, then Go to the Build menu in xcode and clean all Targets and recompile.

Check to see if your HTML file including with your js file.

May be its your trouble for loading js.

2 Comments

actually i upload .zip file from server and download that .zip file in documents with www folder. now i want to open index.html from this path.
Thank you for giving answer. please see my updated answer in Tom Pace's answer. This is work for me.
3

try this ...

i hope this will solve you problem

I also faced the same problem , I solved it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below: you have to add files by choosing create folder references for any folders option

In the figure below one is for adding HTML , click copy also if you are adding file outside from project . if files are already present then no need to copy it again .

enter image description here

2 Comments

i know this method is working good. but i want to open from document\www folder., because my www folder is not fix that's why
try this ..manually create a folder 'document' in your project , then create 'www' folder inside it . then add those folders to your project as folder references . then try above method .

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.