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!
Load Local HTML File to a WKWebViewlet myUrl = myProjectBundle. url(forResource: "my-html-file", withExtension: "html")! where: my-html-file – is the name of HTML file you want to load into WKWebView from your App Bundle.
How to load a HTML string into a WKWebView or UIWebView: loadHTMLString() If you want to load resources from a particular place, such as JavaScript and CSS files, you can set the baseURL parameter to any URL .
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:
Thats it. This worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With