Latest 0.0.1
License MIT
Platforms ios 8.0

[![CI Status]( Preston/WebView-Utilities.svg?style=flat)]( Preston/WebView-Utilities)


To run the example project, clone the repo, and run pod install from the Example directory first.


WebView-Utilities is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "WebView-Utilities"


All functions are exposed through an extension of WKWebView. Don’t forget that the WKWebView should complete navigation before executing any of these. As a result, waiting for didFinishNavigation to fire is recommended.

The following functions are available:

Clear the <head> element

func clearHead(completionHandler: ((AnyObject?, NSError?) -> Void)?)

Removes all HTML from the <head> element of the document currently loaded in the WKWebView.

Add an element to the <head>

func addElementToHead(elementHTML: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Add an element to the <head> element. Specify the HTML of the element, for example:

<meta name="viewport" content="width=device-width, initial-scale=1.0" >

Remove an element from the document

func removeElement(elementId: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Remove an element with the specified ID from the document.

Add CSS to the document

func addCSS(css: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Injects CSS into the document by appending a <style> element to the <head>. For example:

webView.addCSS("body { margin: 20px; } .aClass { margin: 20px; }", completionHandler: nil)

Remove all elements withing <body> except for one

func stripAllOtherElementsFromBody(elementId: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Strips all elements from the document <body>, with the exeception of the element with the specified ID.

Chaining methods

You may want to execute multiple functions that manipulate the DOM. Instead of executing multiple functions and chaining them through their completionHandlers, you can access the Javascript for each function and then concatenate each Javascript snippet:

let js = WKWebView.clearHeadJS() + WKWebView.addCSSJS("body { margin: 20px }")

You can then execute this through the provided convenience method:

webView.executeJavaScript(js, completionHandler: nil)

This is preferred over using evaluateJavascript as it prevents the navigation delegate from firing.


Steven Preston, [email protected]


WebView-Utilities is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "WebView-Utilities",
    "version": "0.0.1",
    "summary": "A library of helper functions to manipulate the DOM within a WKWebView.",
    "description": "A library of helper functions (available through an extension to WKWebView) to manipulate the DOM within a WKWebView. Functions available:n* stripAllOtherElementsFromBody - removes all except one element from HTML body.n* clearHead - removes all inner HTML from the head element.n* addElementToHead - adds an element to the head.n* removeElement - removes an element from the DOM.n* addCSS - add CSS to the document head.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Steven Preston": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.0.1"
    "platforms": {
        "ios": "8.0"
    "source_files": "WebView-Utilities/Classes/**/*"

Pin It on Pinterest

Share This