Swift3 不同视图控制器中的Swift 3 WKWebView打开链接(url)';WKS网络视图
我尝试了下面的代码,但不确定如何将request.url传递给第二个ViewController的WKWebViewSwift3 不同视图控制器中的Swift 3 WKWebView打开链接(url)';WKS网络视图,swift3,wkwebview,Swift3,Wkwebview,我尝试了下面的代码,但不确定如何将request.url传递给第二个ViewController的WKWebView func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { switch navigationType{ case .linkClicked
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType{
case .linkClicked:
self.performSegue(withIdentifier: "mySeque", sender:self)
return false
default:
return true
}
}
您需要将URLRequest和WKWebViewConfiguration从第一个WKWebViewController传递到第二个WKWebViewController
guard let secondURLRequest = secondURLRequest else
{ fatalError("No secondURLRequest") }
guard let secondWebConfiguration = secondWebConfiguration else
{ fatalError("No webConfiguration") }
//WKWebView
seconWKWebView = WKWebView(frame: <yourValue>, configuration: secondWebConfiguration)
请参阅下面的实施
在FirstWKWebViewController中,将全局变量保持为
var firstURLRequest: URLRequest?
假设您的WKWebView是
firstWebView
将代码修改为
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType{
case .linkClicked:
firstURLRequest = request //<---code add
self.performSegue(withIdentifier: "mySeque", sender:self)
return false
default:
return true
}
然后在FirstWKWebViewController中,在segue调用之前通过WebConfiguration和URLReqest
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let segueIdentifier = segue.identifier {
switch segueIdentifier {
case "mySeque":
if let secondWKWebVC = segue.destination as? SecondWKWebViewController {
if nsurlreq != nil { secondWKWebVC.secondURLRequest = nsurlreq! as URLRequest }
secondWKWebVC.secondWebConfiguration = firstWebView.configuration
}
default:
return
}
}
}
然后在第二个WebViewController中
guard let secondURLRequest = secondURLRequest else
{ fatalError("No secondURLRequest") }
guard let secondWebConfiguration = secondWebConfiguration else
{ fatalError("No webConfiguration") }
//WKWebView
seconWKWebView = WKWebView(frame: <yourValue>, configuration: secondWebConfiguration)
guard let secondURLRequest=secondURLRequest else
{fatalError(“无第二个URL请求”)}
guard let secondWebConfiguration=secondWebConfiguration else
{fatalError(“无网络配置”)}
//WKWebView
seconWKWebView=WKWebView(帧:,配置:secondWebConfiguration)
您需要将URLRequest和WKWebViewConfiguration从第一个WKWebViewController传递到第二个WKWebViewController
guard let secondURLRequest = secondURLRequest else
{ fatalError("No secondURLRequest") }
guard let secondWebConfiguration = secondWebConfiguration else
{ fatalError("No webConfiguration") }
//WKWebView
seconWKWebView = WKWebView(frame: <yourValue>, configuration: secondWebConfiguration)
请参阅下面的实施
在FirstWKWebViewController中,将全局变量保持为
var firstURLRequest: URLRequest?
假设您的WKWebView是
firstWebView
将代码修改为
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType{
case .linkClicked:
firstURLRequest = request //<---code add
self.performSegue(withIdentifier: "mySeque", sender:self)
return false
default:
return true
}
然后在FirstWKWebViewController中,在segue调用之前通过WebConfiguration和URLReqest
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let segueIdentifier = segue.identifier {
switch segueIdentifier {
case "mySeque":
if let secondWKWebVC = segue.destination as? SecondWKWebViewController {
if nsurlreq != nil { secondWKWebVC.secondURLRequest = nsurlreq! as URLRequest }
secondWKWebVC.secondWebConfiguration = firstWebView.configuration
}
default:
return
}
}
}
然后在第二个WebViewController中
guard let secondURLRequest = secondURLRequest else
{ fatalError("No secondURLRequest") }
guard let secondWebConfiguration = secondWebConfiguration else
{ fatalError("No webConfiguration") }
//WKWebView
seconWKWebView = WKWebView(frame: <yourValue>, configuration: secondWebConfiguration)
guard let secondURLRequest=secondURLRequest else
{fatalError(“无第二个URL请求”)}
guard let secondWebConfiguration=secondWebConfiguration else
{fatalError(“无网络配置”)}
//WKWebView
seconWKWebView=WKWebView(帧:,配置:secondWebConfiguration)