1

I am programmatically generating HTML and want to link to a CSS file that I've included in my Resources folder. I am using a baseURL of nil, and my CSS file is at the top level of the project file, but this "link" is definitely not working (ie, the CSS file is clearly not being loaded/found when the UIWebView displays the HTML).

Is it possible to do this? Or will I need to put the CSS file on the web somewhere and link to it via a URL? (I can see the advantage of doing this -allowing the app to change its style without redistributing the app).

Thanks

3
  • What does the stylesheet reference line you're using look like? Commented Mar 1, 2010 at 18:08
  • Depending on how you're calling the stylesheet, this article might help you. Post up the code your app is generating if you can. Commented Mar 1, 2010 at 18:10
  • Hi, I'm having this same issue, but the accepted answer actually makes nothing show on my UIWebView (the unstyled html displayed before adding the code). Here is what my reference looks like: NSString *htmlString = [NSString stringWithFormat:@"<html><head><link id=\"stylesheet\" rel=\"stylesheet\" href=\"Css/CommentStyle.css\" type=\"text/css\" /></head><body>%@</body></html>", htmlBody]; Any ideas? I will most likely open a new question, but figured I would use this one since it is the same problem. Thanks. Commented Jun 23, 2010 at 17:02

2 Answers 2

5

Thanks everyone! Here's what I did, works great.

NSBundle *bundle = [NSBundle mainBundle];
NSURL *resourceBaseURL = [NSURL fileURLWithPath:[bundle bundlePath]];
[self.webView loadHTMLString:htmlString baseURL:resourceBaseURL];
Sign up to request clarification or add additional context in comments.

Comments

1

Set the base URL to the bundle path:

NSBundle *bundle = [NSBundle mainBundle];
NSURL *baseURL = [NSURL fileURLWithPath:[bundle bundlePath]];

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.