这个方法对我有用,请一步一步来
示例 Twitter Resonse HTML 外观(删除文本中的 " 以生成正确的字符串)
let responseTwitterHTMLContent = " <blockquote class=\"twitter-tweet\" data-width=\"500\">\n<p lang=\"en\" dir=\"ltr\"><a href=\"https:\/\/twitter.com\/kalyansury\">@kalyansury<\/a> Yes, last night. With a really lame and bleeding obvious response. I'm thinking of my next steps. <a href=\"https:\/\/twitter.com\/HDFC_Bank\">@HDFC_Bank<\/a> <a href=\"https:\/\/twitter.com\/HDFCBank_Cares\">@HDFCBank_Cares<\/a><\/p>\n<p>— Karthik (@beastoftraal) <a href=\"https:\/\/twitter.com\/beastoftraal\/status\/826589530748813313\">February 1, 2017<\/a><\/p><\/blockquote> "
如果您的回复不包含此 twitter 脚本
<script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>
以编程方式在 head 标签中包含如下 Twitter 脚本
<head><script async src=//platform.twitter.com/widgets.js charset=utf-8></script></head>
那么最终修改后的 HTML 内容看起来像
let ModifyHtmlcontent = "<html><head><script async src=//platform.twitter.com/widgets.js charset=utf-8></script><style></head><body style = width: 100%; height:auto; overflow:hidden; margin:100px; text-align:justify;>\(responseTwitterHTMLContent)</body></html>"
在 webview 中加载此内容
如果我在 html 上加载,它不会显示 twitter 格式,因为我们上面包含的 twitter 脚本不是由 uiwebview 下载的,因为您需要提供基本 url https:
let url: URL = URL(string: "https:")!
self.yourWebView.loadHTMLString(ModifyHtmlcontent, baseURL: url)
如果基础 url 产生问题,则更改脚本直接在 twitter 脚本中添加 https: 并将基础 url 设为 nil
<head><script async src=https://platform.twitter.com/widgets.js charset=utf-8></script></head>
self.yourWebView.loadHTMLString(ModifyHtmlcontent, baseURL: nil)
确保您已提供脚本异步
编码愉快!
Reference