2017-02-12 78 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) 
    }