2017-02-12 76 views
0

我有一个选项卡栏控制器,它有两个不同的视图控制器 - 每个控制器都带有一个WKWebView。从另一个类访问webView Swift 3

这是我WebViewController内:(我基本可以破译基于URL,如果我需要的ID扔给其他webView的

HTMLSermonAudioController().webView.load(URLRequest(url: url)) 

是我试图去访问的部分。其他类的web视图改变已加载页面的URL。

if requestURL.absoluteString.hasPrefix("bocaudio://?url="){ 
let url = URL(string: "https://storage.googleapis.com/boc-audio/123.mp3")! 
HTMLSermonAudioController().webView.load(URLRequest(url: url)) 
tabBarController?.selectedIndex = 3 
} 

我也尝试使用内HTMLSermonAudioController等各种方法的类功能上面,但我似乎无法访问

import UIKit 
import WebKit 

class HTMLSermonAudioController: UIViewController, WKNavigationDelegate { 
var webView: WKWebView! 
override func loadView() { 
    webView = WKWebView() 
    webView.navigationDelegate = self 
    view = webView 
    let url = URL(string: "https://www.boc.domain/audioapp/290/")! 
    webView.load(URLRequest(url: url)) 
    webView.allowsBackForwardNavigationGestures = true 
} 


class func test(){ 
    let url = URL(string: "https://www.boc.domain/audioapp/290/")! 
    HTMLSermonAudioController().webView.load(URLRequest(url: url)) 
} 


func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 
    title = webView.title 
} 

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 
    decisionHandler(.allow) 
} 


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

}

因为Webview是在覆盖FUNC的loadView,这对我来说很难对其进行访问。感谢上帝提供混合应用程序,因为只要我克服了这个最后的障碍,我就不用担心任何更快的代码。

回答

1

我最终搞清楚如何与通知中心

NotificationCenter.default.addObserver(self, selector: #selector(HTMLSermonAudioController.connectWithSpecifiedItem), name: urlNotification, object: nil) 


func connectWithSpecifiedItem(notification: NSNotification){ 
    let itemUrl = notification.object as! NSURL 

    //webView.load(URLRequest(url: url)) 
    self.webView!.load(URLRequest(url: itemUrl as URL)) 
} 

做到这一点在其他控制器:

if requestURL.absoluteString.hasPrefix("https://www.place.domain/audioapp/"){ 
     tabBarController?.selectedIndex = 3 
     sendData(url: requestURL) 
     decisionHandler(.cancel) 
     return 
    } 
func sendData(url: URL){ 
     NotificationCenter.default.post(name: HTMLSermonAudioController().urlNotification, object: requestURL) 
    }