How can I load the CSS file with HTML in Swift 3?

Viewed 4645

I have an HTML file which is being displayed fine on my device but it does not load the CSS file in it on my device. When I do open the HTML on my browser, my CSS is working fine.

This is my HTML code:

<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
    <h1>Prateek Bhatt</h1>
    <br>
    <p>is the greatest</p>
</body>

This is my CSS file named stylesheet.css:

body {
    background-color: lightblue;
}

h1 {
    color: navy;
    margin-left: 20px;
}

Also this is the code for Swift 3:

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let htmlFile = Bundle.main.path(forResource: "index", ofType: "html")
        let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
        webView.loadHTMLString(html!, baseURL: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }    
}
1 Answers
Related