Disabling magnification in WKWebView by Paulo Fierro

On a current project we're using the newly introduced (in iOS 8) and mostly awesome WKWebView to show arbitrary content from around the web. Now for some very specific reasons I can't get into, we need to disable the pinch-to-zoom gesture for all the pages that are displayed.

Luckily WKWebView has an allowsMagnification property to do just this, but unfortunately this and the other properties that allow you to modify the magnification are only available on OS X.

But all is not lost, we can attach our own custom Javascript to any page that we load. And with a little help from Javascript we can create a meta tag that disables scaling of the viewport and inject it into the document's <head>.

Once we have this Javascript we can create a WKUserScript object which we tell the WKUserContentController to always inject into loaded pages, and pass that controller to a WKWebViewConfiguration object, which is used when we create our WKWebView.

That sounds like a mouthful but its quite straightforward. Paste code the code below into a UIViewController's viewDidLoad method to try it out.

And the same thing in Swift for funsies: