2017-02-12 7 views
0

私はTab Bar Controllerに2つの異なるView Controllerがあります。それぞれにWKWebViewがあります。別のクラスからwebViewにアクセスするSwift 3

これは私のWebViewController内部にある:(私は基本的にURLに基​​づいて解読することができ、私は他のWebViewにIDをスローする必要がある場合

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

は、私がアクセスするために取得しようとしている部分です。他のクラスのWebViewのは、私もHTMLSermonAudioControllerや他の様々な方法内部クラスの機能を使用して上記を試してみた。

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 
} 

をすでにロードされているページのURLを変更するには、私はちょうど見えることはできませんアクセスするには

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) 
    } 
関連する問題