如何从swift中的UIWebView获取HTML标记值

时间:2017-09-08 06:05:09

标签: ios swift uiwebview

我想在我的标签表单中使用HTML标签显示以下代码中的网址,所以请帮我解决一下

<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) 
 } 

2 个答案:

答案 0 :(得分:1)

let htmlString:String = webView.stringByEvaluatingJavaScript(from: "document.getElementById('link').innerHTML")!

答案 1 :(得分:0)

使用 NSDataDetector 课程可以匹配日期,地址,链接,电话号码和公交信息。 Reference

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!)
}