Created
June 28, 2017 10:15
-
-
Save starhoshi/efde2a0283f05e6a4d32a225617294ab to your computer and use it in GitHub Desktop.
WKWebView Sample
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
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 | |
} | |
} |
This helped me understand how to properly construct a WKUIDelegate
, thank you.
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
[iOS 8] WKWebView で Web ページのタイトルやURLを表示する 〜 Objective-C 編 | Developers.IO
canGoBack などは KVO 監視しておいた方が良さそう