Reputation: 309
i have a view that contains a WebView the idea is when i click on a link in that webview and the link is not from the same domain of the url i use, it will open another view that contains another webview and pass that url to it.
here my first view
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
var url = "http://someurl"
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(webView : UIWebView) {
}
func webView(webView: UIWebView, shouldStartLoadWithRequest r: NSURLRequest, navigationType nt: UIWebViewNavigationType) -> Bool {
var url = webView.request?.URL.absoluteString
if nt == UIWebViewNavigationType.LinkClicked {
self.switchScreen(r)
return false
}
return true
}
func switchScreen(url: NSURLRequest) {
let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("innerBrowser") as UIViewController
vc.url = url
self.presentViewController(vc, animated: true, completion: nil)
}
}
and then the second view
import UIKit
class InnerBrowserController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var innerBrowserView: UIWebView!
var url:NSURLRequest?
override func viewDidLoad() {
super.viewDidLoad()
//let requestURL = NSURL(string:url!)
//let request = NSURLRequest(URL: requestURL!)
self.innerBrowserView.loadRequest(url!)
}
}
when i set the url in the second view it works just fine.
Upvotes: 0
Views: 339
Reputation: 7948
Few things come across my mind.
First, you cast to UIViewController
, instead of casting to InnerBrowserController
. You could do it like :
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("innerBrowser") as InnerBrowserController
The second thing is that it seems you never assign your vc.url
, hence, you don't pass the value to the second view. You can do that like:
let requestURL = NSURL(string:url)
vc.url = NSURLRequest(URL: requestURL!)
The former will work only if you cast your view controller loaded from storyboard into the innerBrowserView
.
Maybe I missed something, but I think this will fix your problem.
Upvotes: 2