使用WKWebView显示嵌入式Twitter时间轴时,某些Tweet链接不起作用

时间:2019-05-31 13:29:04

标签: swift twitter wkwebview

我正在尝试使用WKWebView来显示来自Twitter的一个嵌入式时间轴。

WKWebView成功加载了嵌入内容之后,几乎所有链接都可以使用,但是时间线上显示的每个tweet的重定向链接却无效。

这是嵌入式的tweet链接:

https://publish.twitter.com/?query=%40Google&widget=Timeline

这是嵌入式Twitter时间轴:

< a class="twitter-timeline" href="https://twitter.com/Google?ref_src=twsrc%5Etfw">Tweets by Google< /a> 
< script async src="https://platform.twitter.com/widgets.js" charset="utf-8">< /script>

如果您向下滚动并尝试单击其中一条推文,则会被引导至当前推文,但是当您使用WKWebView显示时,该推文将不起作用。


在测试过程中,我尝试使用相同的嵌入式时间轴,并在运行正常的Android设备上进行了测试。

我认为嵌入式链接很好,我实现WKWebView的方式一定有问题,或者WKWebView本身在显示Twitter嵌入式时间轴方面存在问题。


这是我的代码,用于使用WKWebView显示嵌入式Twitter时间轴:

import UIKit
import WebKit

class ViewController: UIViewController {

    private var mWebView: WKWebView!
    let embeddedTwittContent = "<a class='twitter-timeline' href='https://twitter.com/Google?ref_src=twsrc%5Etfw'>Tweets by Google</a>"
    let scriptValue = "<script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>"
    let redirectLink = "https://twitter.com/Smaforetagarna/status/"


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true

        let configuration = WKWebViewConfiguration()
        configuration.websiteDataStore =  WKWebsiteDataStore.nonPersistent()
        configuration.preferences = preferences

        self.mWebView = WKWebView(frame: CGRect.zero, configuration: configuration)

        self.mWebView.translatesAutoresizingMaskIntoConstraints = false
        self.mWebView.allowsLinkPreview = true
        self.mWebView.allowsBackForwardNavigationGestures = true
        self.mWebView.navigationDelegate = self
        self.view.addSubview(self.mWebView)

        // Add Constraints
        self.mWebView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
        self.mWebView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
        self.mWebView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
        self.mWebView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true

        loadTweetNews()
    }

    func loadTweetNews(){
        let htmlHeader = "<!DOCTYPE html> <html><meta name=\'viewport\' content=\'initial-scale=1.0\'/> <head> \(scriptValue) </head> <body>"
        let htmlFooter = "</body> </html>"
        let orderHtml = htmlHeader + embeddedTwittContent + htmlFooter
        let url: URL = URL(string: "https:")!

        self.mWebView.loadHTMLString(orderHtml, baseURL: url)
    }


}

extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("page finished load")
    }

    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        print("didReceiveServerRedirectForProvisionalNavigation: \(navigation.debugDescription)")
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("didStartProvisionalNavigation")
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated  {
            if let url = navigationAction.request.url,
                UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
                print(url)
                decisionHandler(.cancel)
            } else {
                decisionHandler(.allow)
            }
        } else {
            decisionHandler(.allow)
        }
    }

}

如果运行此代码,则可以单击几乎所有链接,但是一旦您尝试单击这些推文之一,就不会发生任何事情。


我的项目部署目标是10.0

我的Xcode版本为10.2.1

我的Swift版本是4.2

0 个答案:

没有答案