I'm trying to display one embedded content by using the WKWebView and I can not click some of these links. Is there anything I did wrong?
Here is the Link which I get the embedded content from:
https://publish.twitter.com
Here is the embedded content:
<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>
Here is my code:
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 embeddedTwittContent = "<a class=\"twitter-timeline\" href=\"https://twitter.com/Smaforetagarna?ref_src=twsrc%5Etfw\">Tweets by Smaforetagarna</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)
}
}