Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WebViewController
import UIKit
import WebKit
class WebViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var colour: String?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.co.uk")!
webView.load(URLRequest(url: url))
}
func getHexColour() -> String? {
if let colour = colour {
switch colour {
case "Red" : return "#FF0000"
case "Green" : return "#00FF00"
case "Blue" : return "#0000FF"
default : return nil
}
}
return nil
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
guard let colour = getHexColour() else {
return
}
let css = "body { background-color : \(colour) }"
let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);"
webView.evaluateJavaScript(js, completionHandler: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.