6

I have a common css file and some dynamic html text. What I am trying to do is load the css file from the disk and use dynamic html text to display in the UIWebView instead of putting the same css as text over and over again with the html text.

I am trying this way.

var sometext = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"></head>" + "<body><p>some text goes here</p></body></html>"
let mainbundle = NSBundle.mainBundle().bundlePath
let bundleURL = NSURL(fileURLWithPath: mainbundle)
self.webview.loadHTMLString(sometext, baseURL: bundleURL)

Now the problem is css is not getting applied to the html. Is there any one out there who can help, I think I am stuck.

Please help. Thanks!

5
  • You have to look at this question : stackoverflow.com/questions/5053109/… may you get help Commented Jul 11, 2015 at 4:19
  • @AshishKakkad : I have updated my code based on what you said, still css is not getting applied. can you help, please ? Commented Jul 15, 2015 at 20:43
  • please guys I need help on this, I am stuck. Commented Jul 15, 2015 at 20:44
  • 1
    Update for Swift - self.webView.loadHTMLString(sometext, baseURL: NSBundle.mainBundle().bundleURL) Commented Aug 4, 2016 at 17:49
  • @russell Your question worked as answer for me. thanx Commented Aug 3, 2017 at 12:23

2 Answers 2

5

I had the same problem. My code was almost exactly the same as what Russell posted.

Adding my css file to the project was not enough. I had to reference the file in the "Copy Bundle Resources" list in the Build Phase settings.

How to reference the file in XCode 7:

  1. Select the Build Phase tab
  2. Scroll down to the "Copy Bundle Resources" section and add your file. (note: if *.css files don't appear in the list. Click the "Add Other" button)
  3. In the options window choose "Create Folder Reference"

Thats it. This worked for me.

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

1 Comment

How would I know that css has been applied to my web view (bundle resource has several files) @Thu Trinh
0

I had this problem and found that when copying my file into my project I had to select "Create Folder Reference" instead of "Create Group" in the popup window.

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.