WCWC WCWC - 2 years ago 289
Swift Question

WKWebView delegate issue (Xcode 7.3 using Swift)

I am creating an app that is essentially a webview. It uses custom, proprietary fonts that cannot be used in a regular webapp, hence the app wrapper.

It works fine for most purposes but I wanted to check if a URL request loaded properly, to send a 'page not found' message or some such when a bad URL is given or the URL fails to load. For this I believe I need a delegate.

Problem: every time I try to create a delegate (despite trying a few ways I've seen in examples) the app just crashes on load.

(By the way: if I create the webview programmatically rather than in the storyboard, the custom fonts do not load, which for my purposes is a failure of the main requirement. Please keep this in mind if you have a solution)


import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet var containerView: UIView!
var screenEdgeRecognizer: UIScreenEdgePanGestureRecognizer!
//var currentRadius:CGFloat = 0.0

@IBOutlet var webView: WKWebView!

override func viewDidLoad() {

screenEdgeRecognizer = UIScreenEdgePanGestureRecognizer(target: self,
action: #selector(ViewController.goBack(_:)))
screenEdgeRecognizer.edges = .Left

// *** This line commented out to prevent app from crashing!
//self.webView.navigationDelegate = self

webView.hidden = false;
// this allows the UIWebView box to your background color seamlessly.
webView.opaque = false;

// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "http://localhost/web_app/mobile_login.php");

let requestObj = NSURLRequest(URL: url!);

//let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html");
//let requestObj = NSURLRequest(URL: localfilePath!);
//webView.allowsBackForwardNavigationGestures = true;


// **** This code I hope to use but I think requires a delegate in order to work
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

override func preferredStatusBarStyle() -> UIStatusBarStyle {

return UIStatusBarStyle.LightContent

//return UIStatusBarStyle.Default


func goBack(sender: UIScreenEdgePanGestureRecognizer) {




Many thanks for advice.

Answer Source

As far as I know, you cannot yet create a WKWebView in Interface Builder. I believe it has to be created programmatically. Are you using a UIWebView in the storyboard and trying to setup the outlet as a WKWebView? If so, that's why it is crashing. They are different objects altogether.

You could try using UIWebView if you must build it in Storyboard. But I don't know why WKWebView would behave any differently when created programmatically (well it can't be created in IB in the first place).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download