Last active
June 13, 2018 08:17
-
-
Save dobreandl/a86e52dcdad072785eb2a76b933f9f99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func configureWebView() { | |
let wkWebConfig = WKWebViewConfiguration() // We create a web view configuration | |
let wkUserController = WKUserContentController() // We create a user content controller for adding the JS for manipulating the CSS of the web page | |
wkWebConfig.userContentController = wkUserController | |
addHideHeaderScript(wkWebController: wkUserController) // We add the the hide header script | |
// We configure the web view, and add it as a subview to the current view | |
webView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig) | |
webView.navigationDelegate = self // adding the delegate for receiving navigation callbacks (weather a page has loaded or an error occured) | |
view.addSubview(webView) | |
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
} | |
private func addHideHeaderScript(wkWebController: WKUserContentController) { | |
// Add hide header bar script | |
let scriptURL = Bundle.main.path(forResource:”name_of_the_script_file”, ofType: "js") // finds the path for the JS file | |
let scriptContent = try! String.init(contentsOfFile: scriptURL!) // loads the content of the file into a script | |
// Creates a WKUserScript object, with the contents of the .js file, and injects it .atDocumentStart | |
// (injects the script after the document element is created, but before any other content is loaded) | |
// the element should be applied to all frames of the web page not only to the main one | |
let script = WKUserScript(source: scriptContent, injectionTime: .atDocumentStart, forMainFrameOnly: false) | |
// add the user defined script to the user content controller of our WKWebViewConfig | |
wkWebController.addUserScript(script) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment