如何只显示网页的一部分? UIWebView Swift

时间:2017-09-19 10:17:32

标签: ios swift uiwebview

我正在使用Swift中的iOS - 应用程序,我已经完成了我的Android-App。在Android上,我已经找到了一个解决方案来显示网页的部分。这也是我想对我的iOS应用程序做的。

所以,对于那些熟悉android开发的人来说,正是这个解决方案帮助了我:

How to display a part of the webpage on the webview android

但是我问了一个问题,即使你没有Android开发经验,也可以回答这个问题!

你可以假设,正如这个Android解决方案所做的那样,我的网页是darebee.com,在这种情况下可能在锻炼页面上,所以我的意思是:

https://darebee.com/wods.html

我想显示只有一个div,它会显示页面的内容,在这种情况下:

<div class="ja-products-wrapper products wrapper grid products-grid cols-3">

这是我的代码,直到现在(工作,但显示整页):

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL (string: "https://darebee.com/wods.html")
    let request = URLRequest(url: url!)
    webview.loadRequest(request)
}

我知道还有一种方法可以隐藏网页的某些部分,但只显示一个div - 类会更容易,更优雅,更短。

有可能这样做吗?得到一个有用的答案会很高兴。

更新

我找到了一个可能对我有帮助的解决方案,但不知怎的,它不起作用。 因为代码是2.5岁,我弃用了所以我不得不将代码更新为我自己的代码。 也许在约束部分,可能存在错误,但根本没有错误。尽管打印了我想要打印的所有行,但backgroundColor也不可见。 也许您可以查看我的代码,如果它对您有帮助,请将其与我找到的解决方案进行比较:How to only display part of web page content (swift)

这是我的代码:

import UIKit
import JavaScriptCore
import WebKit

class ViewController: UIViewController {

private weak var webView: WKWebView!


@IBOutlet weak var vclabel: UIView!

private var userContentController: WKUserContentController!


override func viewDidLoad() {
    super.viewDidLoad()
    createViews()
    print("no prob1")
    loadPage(urlString: "https://dribbble.com/", partialContentQuerySelector: ".dribbbles.group")
    print("no prob2")
}

private func createViews() {
    userContentController = WKUserContentController()

    let configuration = WKWebViewConfiguration()
    configuration.userContentController = userContentController

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

    let color1 = hexStringToUIColor(hex: "#FFFFFF")
    webView.backgroundColor = color1
    if (webView.backgroundColor == color1){
        print("color1")
    }
    let leadingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
    let topConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
    let trailingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
    let bottomConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: vclabel, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
    view.addSubview(webView)
NSLayoutConstraint.activate([leadingConstraint, topConstraint, trailingConstraint, bottomConstraint])
    self.webView = webView
}

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

    userContentController.addUserScript(userScript)

    let url = NSURL(string: urlString)!
    webView.load(NSURLRequest(url: url as URL) as URLRequest)
    print("loaded")
}

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

func hexStringToUIColor (hex:String) -> UIColor {
    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

    if (cString.hasPrefix("#")) {
        cString.remove(at: cString.startIndex)
    }

    if ((cString.characters.count) != 6) {
        return UIColor.gray
    }

    var rgbValue:UInt32 = 0
    Scanner(string: cString).scanHexInt32(&rgbValue)

    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

}

这是我的输出:

Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
color1
no prob1
load
DOMSelector
loaded
no prob2

我只是不知道我还应该做些什么才能得到这个问题的答案。 在那里谁能以任何方式帮助我?

1 个答案:

答案 0 :(得分:0)

像这样使用LoadHtmlString:

 let requestURL: URL? = yourWebView.request().url
 var error: Error?
 let page = try? String(contentsOf: requestURL, encoding: String.Encoding.ascii)
 yourWebView.loadHTMLString(page!, baseURL: requestURL)