dwinnbrown dwinnbrown - 6 months ago 58
iOS Question

Swift - Opening web view links modally

So I have a web view displaying a page and would like to open any links in a new view controller containing a web view modally (like twitter and Facebook do).

I have worked out how to get the url of the link clicked:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let youClicked = request.URL!
return true

But haven't managed to work out how I can pass this value into a web view on another page and present that view controller modally as a popover.

Any ideas?


There isn't an out of the box component which can deal with your need. Instead you have to create your own component:

  1. Create a new UIViewController and place a WKWebView in it (Can be archived by Interface Builder or Code - I usually prefer Interface Builder) . Furthermore create an outlet (e.g. called "webView") for the WKWebView

  2. Create the controller - set the URL and present it // Create the controller let controller = storyboard.instantiateViewControllerWithIdentifier("MyCustomWebViewController") //start loading the URL controller.webView.loadRequest(request) // present it presentViewController(viewController, animated: true, completion: nil)