How to import css file into UIWebView when I use loadHTMLString () method in swift (iOS)

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

I try so.

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 css problem does not apply to html. Is there anyone there who can help, I think I'm stuck.

Please, help. Thanks!

+6
source share
2 answers

I had the same problem. My code was almost the same as published by Russell.

Adding my css file to the project was not enough. I had to refer to the file in the "Copy Copy Resources" list in the build phase settings.

How to link to a file in Xcode 7:

  • Select the Build Phase tab
  • Scroll down to the "Copy Resources" section and add the file. (note: if * .css files do not appear in the list. Click the "Add another" button)
  • In the options window, select Create Link to Folder

Here it is. It worked for me.

+5
source

I had this problem and it turned out that when copying my file to the project, I had to select "Create a link to a folder" instead of "Create a group" in a pop-up window.

0
source

All Articles