Created
January 27, 2015 14:26
-
-
Save paulofierro/5b642dcde5ee9e86a130 to your computer and use it in GitHub Desktop.
Disable magnification in WKWebView using Swift
This file contains hidden or 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
// 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")!)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// 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")!))