从WKWebView获得链接吗?

时间:2019-01-26 12:41:00

标签: swift wkwebview

我不知道代码中的问题,并且不显示视频 通过WKWebView链接。

您已经创建了一个新的webview,并使用了tweet链接,而我做到了 他的背部操作能够查看该网站,因为 链接视频,我在后台播放了。

enter image description here

enter image description here

import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate {

    @IBOutlet weak var webview: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        webview.navigationDelegate = self
        let urlstr = "https://twitter.com/A777H_/status/1087074457431277576"
        let request = URLRequest(url: URL(string: urlstr) as! URL)

        webview.load(request)
        let config = WKWebViewConfiguration()
        config.suppressesIncrementalRendering = true
        config.allowsInlineMediaPlayback = true
        config.mediaTypesRequiringUserActionForPlayback = .all
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true

        webview.configuration.preferences = preferences
        webview.configuration.suppressesIncrementalRendering = true
        webview.configuration.allowsInlineMediaPlayback = true
        webview.configuration.mediaTypesRequiringUserActionForPlayback = .all

        self.webview.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

        if webview.isLoading {

        }else{
            print("\(webview.title)")
        }



    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let urlStrn = navigationAction.request.url?.absoluteString {
            //urlStr is what you want
            print(urlStrn)
        }

        decisionHandler(.allow)
    }

}

0 个答案:

没有答案