0
votes

I have a Tab Bar Controller that has two different view controllers -- each with a WKWebView in them.

This is inside my WebViewController: (I basically can decipher based on the URL if I need to throw the ID to the other webView.

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.

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

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.

1

1 Answers

1
votes

I ended up figuring out how to do it with 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)
    }