gpt4 book ai didi

ios - 如何只显示部分网页内容(swift)

转载 作者:搜寻专家 更新时间:2023-11-01 06:46:09 25 4
gpt4 key购买 nike

我是编码新手(完全没有任何编程语言背景)。我正在努力学习快速。我想创建一个简单的天气应用程序,在用户进入城市后以文本形式显示天气信息。我正在从 weather-forecast.com 获取内容。我已经弄清楚如何加载网页内容,但我只想显示页面内容的一个片段(一段),而不是整个页面。有人可以告诉我该怎么做吗?

{
import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet weak var cityText: UITextField!

@IBOutlet weak var webContent: UIWebView!

@IBAction func GoButtonPressed(sender: AnyObject) {

let url = NSURL (string: "http://www.weather-forecast.com");
let requestObj = NSURLRequest(URL: url!);
webContent.loadRequest(requestObj);
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
}

最佳答案

这是一个简单的 View Controller 示例,它只加载网站 dribble.com 的一部分。 Controller 具有选择 DOM 元素并仅显示该元素的方法。它非常简单,但功能强大,足以展示您可以如何在此方面进一步开展工作。

import UIKit
import JavaScriptCore
import WebKit

class TestViewController: UIViewController {

private weak var webView: WKWebView!

private var userContentController: WKUserContentController!

override func viewDidLoad() {
super.viewDidLoad()
createViews()

loadPage("https://dribbble.com/", partialContentQuerySelector: ".dribbbles.group")
}

private func createViews() {
userContentController = WKUserContentController()

let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController

let webView = WKWebView(frame: view.bounds, configuration: configuration)
webView.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(webView)

let views: [String: AnyObject] = ["webView": webView, "topLayoutGuide": topLayoutGuide]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[topLayoutGuide][webView]|", options: .allZeros, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[topLayoutGuide][webView]|", options: .allZeros, metrics: nil, views: views))


self.webView = webView
}

private func loadPage(urlString: String, partialContentQuerySelector selector: String) {
userContentController.removeAllUserScripts()
let userScript = WKUserScript(source: scriptWithDOMSelector(selector),
injectionTime: WKUserScriptInjectionTime.AtDocumentEnd,
forMainFrameOnly: true)

userContentController.addUserScript(userScript)

let url = NSURL(string: urlString)!
webView.loadRequest(NSURLRequest(URL: url))
}

private func scriptWithDOMSelector(selector: String) -> String {
let script =
"var selectedElement = document.querySelector('\(selector)');" +
"document.body.innerHTML = selectedElement.innerHTML;"
return script
}

}

上面示例中显示的 View Controller 仅加载 dribble 网站内的照片、设计部分。

关于ios - 如何只显示部分网页内容(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30581935/

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