Skip to content

Instantly share code, notes, and snippets.

@paulofierro
Created January 27, 2015 14:26
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulofierro/5b642dcde5ee9e86a130 to your computer and use it in GitHub Desktop.
Save paulofierro/5b642dcde5ee9e86a130 to your computer and use it in GitHub Desktop.
Disable magnification in WKWebView using Swift
// Remember to @import WebKit at the top of the class
// Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into <head>
let source: NSString = "var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
"var head = document.getElementsByTagName('head')[0];" +
"head.appendChild(meta);";
let script: WKUserScript = WKUserScript(source: source, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
// Create the user content controller and add the script to it
let userContentController: WKUserContentController = WKUserContentController()
userContentController.addUserScript(script)
// Create the configuration with the user content controller
let configuration: WKWebViewConfiguration = WKWebViewConfiguration()
configuration.userContentController = userContentController
// Create the web view with the configuration
let webView: WKWebView = WKWebView(frame: CGRectZero, configuration: configuration)
webView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(webView)
// Add the constraints
let views: NSDictionary = ["webView" : webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://apple.com")!))
@soltrinox
Copy link

Edit in SWIFT2 for proper cast of webView
let views: [String: AnyObject] = ["webView" : webView!]

comment out

// self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))
// self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))

@amanbolat
Copy link

UPDATE:
UserContentController should be added to WKWebViewConfiguration

      let source: String = "var meta = document.createElement('meta');" +
      "meta.name = 'viewport';" +
      "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
      "var head = document.getElementsByTagName('head')[0];" + "head.appendChild(meta);";
      let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
      let userContentController: WKUserContentController = WKUserContentController()
      let conf = WKWebViewConfiguration()
      conf.userContentController = userContentController
      userContentController.addUserScript(script)
      let webView = WKWebView(frame: CGRect.zero, configuration: conf)

@lsasubilli
Copy link

// Remember to @import WebKit at the top of the class
// Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into
let source: NSString = "var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
"var head = document.getElementsByTagName('head')[0];" +
"head.appendChild(meta);";
let script: WKUserScript = WKUserScript(source: source, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)

// Create the user content controller and add the script to it
let userContentController: WKUserContentController = WKUserContentController()
userContentController.addUserScript(script)

// Create the configuration with the user content controller
let configuration: WKWebViewConfiguration = WKWebViewConfiguration()
configuration.userContentController = userContentController

// Create the web view with the configuration
let webView: WKWebView = WKWebView(frame: CGRectZero, configuration: configuration)
webView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(webView)

// Add the constraints
let views: NSDictionary = ["webView" : webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))

webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://apple.com")!))// Remember to @import WebKit at the top of the class
// Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into
let source: NSString = "var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
"var head = document.getElementsByTagName('head')[0];" +
"head.appendChild(meta);";
let script: WKUserScript = WKUserScript(source: source, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)

// Create the user content controller and add the script to it
let userContentController: WKUserContentController = WKUserContentController()
userContentController.addUserScript(script)

// Create the configuration with the user content controller
let configuration: WKWebViewConfiguration = WKWebViewConfiguration()
configuration.userContentController = userContentController

// Create the web view with the configuration
let webView: WKWebView = WKWebView(frame: CGRectZero, configuration: configuration)
webView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(webView)

// Add the constraints
let views: NSDictionary = ["webView" : webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: .AlignAllLeft, metrics: nil, views: views))

webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://apple.com")!))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment