我使用WKWebView登录网站,现在我想解析该网站的HTML。我如何在Swift中访问网站的HTML?我知道在UIWebView中如何操作,但不知道在WKWebView中如何操作。
谢谢您的帮助!
webView.evaluateJavaScript("document.documentElement.outerHTML.toString()") {
print(html)
}
let script = WKUserScript(source: javascriptString, injectionTime: injectionTime, forMainFrameOnly: true)
userContentController.addUserScript(script)
self.webView.configuration.userContentController.addScriptMessageHandler(self, name: "didGetHTML")
…
func userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage) {
guard message.name == "didGetHTML",
let html = message.body as? String else {
return
}
print(html)
}
webkit.messageHandlers.didGetHTML.postMessage(document.documentElement.outerHTML.toString());
Swift <-> WKWebView
从WKWebView获取HTML内容
wkWebView.evaluateJavaScript("document.body.innerHTML", completionHandler: { (value: Any!, error: Error!) -> Void in
if error != nil {
//Error logic
return
}
//let result = value as? String
//Main logic
})
//Do not forget to extend a class from `WKNavigationDelegate`
func someFunction() {
let wkWebView = WKWebView()
wkWebView.loadHTMLString("<html><body></body></html>", baseURL: nil)
wkWebView.navigationDelegate = self as? WKNavigationDelegate
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//ready to be processed
}
我在这里试图获取DROPBOX新API的令牌并获得结果的线索。(我正在实现他们的流程,但不使用他们的SDK)
现在Dropbox使用网页作为登录页面,并回调YOUR url以便您可以处理令牌。
import WebKit
import SwiftUI
// some code from:
// https://benoitpasquier.com/create-webview-in-swiftui/
// THX pasquier!
let APP_KEY = "YOUR APP KEY"
let REDIRECT_URI = "<YOUR SITE>.dropbox_auth.php"
let DB_URL = "https://www.dropbox.com/1/oauth2/authorize?client_id=APP_KEY&token_access_type=offline&response_type=code&redirect_uri=REDIRECT_URI"
class MyWKDelegate: NSObject, WKNavigationDelegate{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("End loading")
webView.evaluateJavaScript("document.body.innerHTML", completionHandler: { result, error in
if let html = result as? String {
print(html)
}
})
}
}
struct WebView: UIViewRepresentable {
typealias UIViewType = WKWebView
let webView: WKWebView
func makeUIView(context: Context) -> WKWebView {
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) { }
}
class WebViewModel: ObservableObject {
let webView: WKWebView
let url: URL!
let delegate = MyWKDelegate()
init() {
webView = WKWebView(frame: .zero)
webView.navigationDelegate = delegate
let urlStr = DB_URL.replacingOccurrences(of: "APP_KEY", with: APP_KEY).replacingOccurrences(of: "REDIRECT_URI", with: REDIRECT_URI)
print(urlStr)
url = URL(string: urlStr)
loadUrl()
}
func loadUrl() {
webView.load(URLRequest(url: url))
}
}
将答案1和3结合起来对我很有帮助:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("End loading")
webView.evaluateJavaScript("document.documentElement.outerHTML", completionHandler: { result, error in
if let datHtml = result as? String {
print(datHtml)
// parse datHtml here
}
} )
}