如何只显示部分网页内容(swift) [英] How to only display part of web page content (swift)

查看:154
本文介绍了如何只显示部分网页内容(swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am brand new to coding (no background in any programming language at all). I am trying to learn swift. I am wanting to create a simple weather app that displays weather information in text once the user enters a city. I am grabbing the content from weather-forecast.com. I have figured out how to load the web content, but I want to only display a snippet (one paragraph) of the content from the page, not the whole page. Can someone please show me how to do that?

{
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.
    }    
}
}

推荐答案

这是一个简单的视图控制器示例,它仅加载网站 dribble.com 的一部分.控制器具有选择 DOM 元素并仅显示该元素的方法.它非常简单,但功能强大,足以展示您如何进一步开展工作.

Here is an example of simple view controller which loads only part of the website dribble.com. The controller has method to select the DOM element and only show that element. It is quite simple, yet powerful enough to show how you could work further on this.

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
    }

}

上例中的视图控制器只加载照片,dribble 网站内的设计部分.

The view controller shown in the example above only loads photos, design section inside the dribble website.

这篇关于如何只显示部分网页内容(swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆