-
-
Save paulofierro/5b642dcde5ee9e86a130 to your computer and use it in GitHub Desktop.
// 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")!)) |
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)
// 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")!))
Edit in SWIFT2 for proper cast of webView
let views: [String: AnyObject] = ["webView" : webView!]
comment out