Skip to content

Instantly share code, notes, and snippets.

@starhoshi
Created June 28, 2017 10:15
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 starhoshi/efde2a0283f05e6a4d32a225617294ab to your computer and use it in GitHub Desktop.
Save starhoshi/efde2a0283f05e6a4d32a225617294ab to your computer and use it in GitHub Desktop.
WKWebView Sample
import WebKit
import UIKit
class WebViewController: UIViewController {
var url: URL!
var wKWebView: WKWebView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var goBackButton: UIBarButtonItem!
@IBOutlet weak var goForwardButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
wKWebView = WKWebView(frame: createWKWebViewFrame(size: view.frame.size))
containerView.addSubview(wKWebView)
wKWebView.navigationDelegate = self
wKWebView.uiDelegate = self
var request = URLRequest(url: url)
wKWebView.load(request)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
wKWebView.frame = createWKWebViewFrame(size: size)
}
@IBAction func onTappedGoBack(_ sender: Any) {
wKWebView.goBack()
}
@IBAction func onTappedGoForward(_ sender: Any) {
wKWebView.goForward()
}
}
extension WebViewController {
fileprivate func createWKWebViewFrame(size: CGSize) -> CGRect {
let navigationHeight: CGFloat = 60
let toolbarHeight: CGFloat = 44
let height = size.height - navigationHeight - toolbarHeight
return CGRect(x: 0, y: 0, width: size.width, height: height)
}
}
extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// show indicator
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// dismiss indicator
// if url is not valid {
// decisionHandler(.cancel)
// }
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// dismiss indicator
goBackButton.isEnabled = webView.canGoBack
goForwardButton.isEnabled = webView.canGoForward
navigationItem.title = webView.title
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
// show error dialog
}
}
extension WebViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
}
@starhoshi
Copy link
Author

@ptvandi
Copy link

ptvandi commented Apr 1, 2018

This helped me understand how to properly construct a WKUIDelegate, thank you.

@vinodsupnekar
Copy link

It helped me to implement WKWebView with code placed in viewDidLoad method instead of loadView as suggested in Apple Documents.

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