【发布时间】:2017-04-19 12:52:29
【问题描述】:
我正在使用UIWebView 加载作为文章的网页。一旦我按下一个按钮,我就会用url、title 和image 创建一个结构。要访问标题,我需要访问网页的源代码并从每篇文章具有相同类 ID (class="c-page-title") 的 h1 header 获取标题。有人知道如何使用stringByEvaluatingJavaScript 或evaluateJavaScript 函数从h1 header 获取文本吗?
这是我的一些代码供参考:
结构:
struct Article {
var url: URL
var img: URL
var title: String
init(url: URL, img: URL, title: String) {
self.url = url
self.img = img
self.title = title
}
}
填充文章结构的函数:
@IBAction func bookmarkArt(_ sender: Any) {
let articleInst = Article(url: (mainWebView.request?.url)!, img: (mainWebView.request?.url)!, title: "Article Here")
bookmarks.append(articleInst)
print("array: \(bookmarks)")
let alertController = UIAlertController(title: "Bookmark added!", message: "This article was just added to your bookmarks.", preferredStyle: UIAlertControllerStyle.alert)
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "Awesome, thanks!", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
任何帮助将不胜感激!!提前非常感谢。
干杯, 西奥
【问题讨论】:
标签: javascript ios swift xcode uiwebview