Skip to content

Instantly share code, notes, and snippets.

@Ceri-anne
Last active November 25, 2017 20:19
Show Gist options
  • Save Ceri-anne/3b8f9216664dc6b51f67229dc0266e7c to your computer and use it in GitHub Desktop.
Save Ceri-anne/3b8f9216664dc6b51f67229dc0266e7c to your computer and use it in GitHub Desktop.
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