36
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WKWebViewで読み込んでいるHTMLソースを取得する方法

Last updated at Posted at 2015-01-01

WKWebViewの例が見当たらなかったのでメモ。
UIWebViewの例も併記しておきます。

HTMLソースを取得するならどっちでも良さそうですけど、
他のことするならクロージャで処理できるWKWebViewの方が良さそうです。

WKWebViewの場合

var webView = WKWebView()
webView.evaluateJavaScript("document.body.innerHTML", completionHandler: { (html, error) -> Void in
}

UIWebViewの場合

var uiview = UIWebView()
let html = uiview.stringByEvaluatingJavaScriptFromString("document.body.innerHTML")

bodyだけじゃなくて全体が欲しい場合

"document.body.innerHTML"
"document.getElementsByTagName('html')[0].innerHTML"に書き換えます。
この部分はJavaScriptなので必要に応じて書き換えればいろいろできます。

36
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?