0

I am working on my iOS-App in Swift and I've already finished my Android-App. On Android, I already found a solution to display just a part of a webpage. This is what I want to do to my iOS-app, too.

So, for those who are familiar with android developing, it was this solution which helped me:

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

But I asked my question in a way that it is possible to answer this question even if you don't have experience with android developing!

You can assume that, as this android solution does, my webpage is darebee.com, in this case maybe on the workout-page, so I precisely mean:

https://darebee.com/wods.html

I want to show just one div class which shows the content of the page, in this case:

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

That is my code until now (working, but showing full page):

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

I know there is also a way to hide some parts of the webpage, but it would be far more easier, elegant and shorter to just show this one div-class.

Is it possible to do this? It would be nice to get an answer which is helpful.

UPDATE:

I found a solution that might be helpful for me, but somehow it is not working. Because the code was 2,5 years old, something I deprecated so I had to update the code to my own. Maybe on the constraints part, there could be a mistake, but there are no errors at all. Also the backgroundColor is not visible although all the lines I wanted to print are printed. Maybe you can look to my code and, if it helps you, compare it to the solution I found: How to only display part of web page content (swift)

This is my code:

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)
    )
}

}

And this is my output:

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

I just don't know what else I should do to get an answer to this question. Anyone out there who can help me in any way?

linuxjole
  • 23
  • 1
  • 6

1 Answers1

0

Use LoadHtmlString like this:

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