gpt4 book ai didi

swift - 即使点击事件后,如何从 URL 加载部分 HTML?

转载 作者:行者123 更新时间:2023-11-30 12:26:17 25 4
gpt4 key购买 nike

我编写了一些代码来从 url 加载所有 html 并解析它以删除 header 。所以我在 header 下显示了 html 的其余部分。但是,单击正文 html 中的事件后,屏幕会显示 URL 中的完整 html。

有什么解决办法吗?或者我在处理这个问题时犯了一个错误吗?

我编写的代码如下

import UIKit
import Fuzi

class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

let myURLString = "http://yahoo.com"
var myHTMLString = ""
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}

do {
myHTMLString = try String(contentsOf: myURL, encoding: .utf8)
} catch let error {
print("Error: \(error)")
}

do {
// if encoding is omitted, it defaults to NSUTF8StringEncoding
let doc = try! HTMLDocument(string: myHTMLString, encoding: String.Encoding.utf8)

let fullHtml:String = (doc.firstChild(xpath: "//*")?.rawXML)!


if let header = doc.firstChild(xpath: "//body/div/header") {

let headerString:String = header.rawXML

let withoutHeader = fullHtml.replacingOccurrences(of: headerString, with: "")

webView.loadHTMLString(withoutHeader as String, baseURL: nil)
}
} catch let error{
print(error)
}

}

最佳答案

试试这个,它适用于 UIWebViewNavigationType.linkClicked。您可以修改它以与其他 UIWebViewNavigationType 一起使用。

import UIKit
import Fuzi

class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

webView.delegate = self

let myURLString = "http://yahoo.com"
var myHTMLString = ""
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}

do {
myHTMLString = try String(contentsOf: myURL, encoding: .utf8)
} catch let error {
print("Error: \(error)")
}

do {
// if encoding is omitted, it defaults to NSUTF8StringEncoding
let doc = try! HTMLDocument(string: myHTMLString, encoding: String.Encoding.utf8)

let fullHtml:String = (doc.firstChild(xpath: "//*")?.rawXML)!


if let header = doc.firstChild(xpath: "//body/div/header") {

let headerString:String = header.rawXML

let withoutHeader = fullHtml.replacingOccurrences(of: headerString, with: "")

webView.loadHTMLString(withoutHeader as String, baseURL: nil)
}
} catch let error{
print(error)
}
}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked
{
let myURLString = webView.request?.url.absoluteString
webView.stopLoading()
var myHTMLString = ""
let myURL = URL(string: myURLString)
do {
myHTMLString = try String(contentsOf: myURL, encoding: .utf8)
} catch let error {
print("Error: \(error)")
}

do {
// if encoding is omitted, it defaults to NSUTF8StringEncoding
let doc = try! HTMLDocument(string: myHTMLString, encoding: String.Encoding.utf8)

let fullHtml:String = (doc.firstChild(xpath: "//*")?.rawXML)!


if let header = doc.firstChild(xpath: "//body/div/header") {

let headerString:String = header.rawXML

let withoutHeader = fullHtml.replacingOccurrences(of: headerString, with: "")

webView.loadHTMLString(withoutHeader as String, baseURL: nil)
}
} catch let error{
print(error)
}
}
return true
}

关于swift - 即使点击事件后,如何从 URL 加载部分 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com