首页 文章

从另一个类Swift 3访问webView

提问于
浏览
0

我有一个标签栏控制器,它有两个不同的视图控制器 - 每个都有一个WKWebView .

这是在我的WebViewController中:(如果我需要将ID丢给其他webView,我基本上可以根据URL进行解密 .

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

是我试图访问其他类的webView以更改已加载的页面的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 回答

  • 1

    我最终想弄清楚如何使用NotificationCenter

    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)
        }
    

相关问题