我正在使用UIWebView加载一个网页
let urlStr = "http://duckduckgo.com"
let urlReq = NSMutableURLRequest(URL: NSURL(string: urlStr)!)
webView.loadRequest(urlReq)
然后,在页面加载完成后,我想访问HTML内容。
func webViewDidFinishLoad(webView: UIWebView) {
let href = webView.stringByEvaluatingJavaScriptFromString("window.location.href")
println("window.location.href = \(href)")
let doc = webView.stringByEvaluatingJavaScriptFromString("document")
println("document = \(doc)")
}
但是document仅返回一个空字符串(Optional("")
)。而window.location.href部分运行正常。我做错了什么?