With the following code, I was able to display my Navigation Bar build on Storyboard, however, andy URL links would not open and were dead. I found my ViewController needed a way to handle using a decision handler. (SEE WHAT DID I TRY SECTION)
import UIKit import WebKit
class SecondViewController: UIViewController {
@IBOutlet weak var webViewTwo: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://on.sprintful.com/dr-clutter-junk")
webViewTwo.load(URLRequest(url: url!))
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
SECTION: WHAT DID I TRY
With the following code, the links work but the Nav Bar from StoryBoard does not display. I believe the is the IB Outlet reference is the issue. When I go to edit the
"func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler:" and alter it to "func webViewTwo(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler:" the nav bar wont show and links do not work.
import UIKit import WebKit
class SecondViewController: UIViewController, WKNavigationDelegate { @IBOutlet var webViewTwo: WKWebView!
override func loadView() {
webViewTwo = WKWebView()
view = webViewTwo
let url = URL(string: "https://calendly.com/drclutterjunk")!
webViewTwo.load(URLRequest(url: url))
webViewTwo.allowsBackForwardNavigationGestures = true
webViewTwo.navigationDelegate = self
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, host.hasPrefix("dttps://calendly.com") !=
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
decisionHandler(.cancel)
} else {
// Open in web view
decisionHandler(.allow)
}
} else {
// other navigation type, such as reload, back or forward buttons
decisionHandler(.allow)
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
}