Ios 如何在swift中从UIWebView获取HTML标记值
我想在我的标签表单中显示URL,下面的代码使用HTML标记,所以请帮助我解决它Ios 如何在swift中从UIWebView获取HTML标记值,ios,swift,uiwebview,Ios,Swift,Uiwebview,我想在我的标签表单中显示URL,下面的代码使用HTML标记,所以请帮助我解决它 <p> <span id="link" style="word-break:break-all;" ng-bind="link"></span> </p> func webViewDidFinishLoad(_ webView: UIWebView) { let htmlString = liveWebView.stringByEvaluatingJavaSc
<p> <span id="link" style="word-break:break-all;" ng-bind="link"></span> </p>
func webViewDidFinishLoad(_ webView: UIWebView)
{
let htmlString = liveWebView.stringByEvaluatingJavaScript(from: "window.someGlobalVariable")!
htmlString = webView.stringByEvaluatingJavaScript(from: "document.getElementById('Start').disabled = false;")!
print("******",htmlString)
}
func webViewDidFinishLoad(webView:UIWebView)
{
让htmlString=liveWebView.stringByEvaluatingJavaScript(from:“window.someGlobalVariable”)!
htmlString=webView.stringByEvaluatingJavaScript(from:“document.getElementById('Start')。disabled=false;”)!
打印(“*******”,htmlString)
}
使用NSDataDetector
类可以匹配日期、地址、链接、电话号码和交通信息
让htmlString=“\n”
let类型:NSTextCheckingType=.Link
让我试试?NSDataDetector(类型:types.rawValue)
防护装置let detect=探测器else{
返回
}
让matches=detect.matchesisnstring(htmlString,选项:.ReportCompletion,范围:NSMakeRange(0,htmlString.characters.count))
一场接一场的比赛{
打印(match.URL!)
}
感谢各位,我终于找到了解决方案:htmlString=liveWebView.stringByEvaluatingJavaScript(from:“document.getElementById('link').innerHTML”)!
let htmlString = "<p><a href=\"https://www.youtube.com/watch?v=i2yscjyIBsk\">https://www.youtube.com/watch?v=i2yscjyIBsk</a></p>\n"
let types: NSTextCheckingType = .Link
let detector = try? NSDataDetector(types: types.rawValue)
guard let detect = detector else {
return
}
let matches = detect.matchesInString(htmlString, options: .ReportCompletion, range: NSMakeRange(0, htmlString.characters.count))
for match in matches {
print(match.URL!)
}
let htmlString:String = webView.stringByEvaluatingJavaScript(from: "document.getElementById('link').innerHTML")!