I have a Tab Bar Controller that has two different view controllers -- each with a WKWebView in them.
我有一个标签栏控制器,它有两个不同的视图控制器 - 每个都有一个WKWebView。
This is inside my WebViewController: (I basically can decipher based on the URL if I need to throw the ID to the other webView.
这是在我的WebViewController中:(如果我需要将ID抛给其他webView,我基本上可以根据URL进行解密。
HTMLSermonAudioController().webView.load(URLRequest(url: url))
is the part I'm trying to get to access the webView of the other Class to change the URL of a page that is already loaded.
是我试图访问其他类的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
}
I've also tried the above with using a class function inside HTMLSermonAudioController and other various methods, but I just can't seem to access
我也尝试过使用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.
}
}
Because the webView is in the override func loadView, it's hard for me to access it. Thank God for hybrid apps, because as soon as I get over this last hurdle, I won't have to worry about any more swift code.
因为webView位于覆盖功能loadView中,所以我很难访问它。感谢上帝的混合应用程序,因为一旦我克服了最后一个障碍,我就不用担心任何更快速的代码了。
1 个解决方案
#1
1
I ended up figuring out how to do it with NotificationCenter
我最终想弄清楚如何使用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))
}
In other Controller:
在其他控制器中:
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)
}
#1
1
I ended up figuring out how to do it with NotificationCenter
我最终想弄清楚如何使用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))
}
In other Controller:
在其他控制器中:
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)
}