Reputation: 15
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)
Code:
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() {
super.viewDidLoad()
screenEdgeRecognizer = UIScreenEdgePanGestureRecognizer(target: self,
action: #selector(ViewController.goBack(_:)))
screenEdgeRecognizer.edges = .Left
view.addGestureRecognizer(screenEdgeRecognizer)
// *** 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.loadRequest(requestObj);
//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() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
//LightContent
return UIStatusBarStyle.LightContent
//Default
//return UIStatusBarStyle.Default
}
func goBack(sender: UIScreenEdgePanGestureRecognizer) {
//
self.webView.goBack()
}
}
Many thanks for advice.
Upvotes: 0
Views: 741
Reputation: 15
Mike Cole's answer above got to the heart of what my problem was. But also FYI on further investigation, I discovered this question which looks at custom fonts and the difficulties using them with WKWebViews
Upvotes: 0
Reputation: 1301
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).
Upvotes: 1