3015

How to load part of HTML from URL even after clicking event?

Question:

I have wrote some code to load all html from url and parse it to remove header. So I got the rest of the html under header to show. However, after clicking event in the body html, the screen shows full html from the URL.

Is there any solution for this? or Did I make a mistake to approach this problem?

The code I made is below

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

Answer1:

Try this, it works with UIWebViewNavigationType.linkClicked. You can modify it to use with other 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 }

Answer2:

I found an answer!

Thanks to @Sherman for the hint to solve this problem.

I had to replace a line of code below

if navigationType == .linkClicked

to

if navigationType == UIWebViewNavigationType.linkClicked

Then It works!

Recommend

  • iphone UIWebView - Force show scrollbar always
  • UIWebview manipulating SVG 'on the fly'
  • Proper instantiation of UISearchDisplayController
  • IOS - how to hide keyboard if user clicks on randome elements and not background?
  • Load custom view in phonegap/iphone
  • Swift: How to return to the same instance of my UIViewController
  • fatal error: init(coder:) has not been implemented Xcode 7 iOS 9
  • Segue with Push animation Right to Left
  • Adding padding and border to an UIImageView
  • Saving an array of NSURL to NSUserDefaults
  • UINavigationController alternatives
  • UITableView without still works!
  • String not fitting in label which is in a subview for swift
  • WebView: webpage not available but I load it from an html string
  • View Controller Loads Twice - How Do I Fix It?
  • UIBarButtonItem - Argument of '#selector' cannot refer to local function - Swift 3
  • updatesearchresultsforsearchcontroller not called
  • Phonegap Android App, open links within app?
  • windowScriptObject method not found on Objective-C code
  • iOS, How to check whether the user likes a facebook page or not?
  • Printing input from TextField to a Label in Xcode with Swift
  • Auto Height of UICollectionView inside UITableViewCell
  • input type=“file” accept=“image/*” doesn't work in phone gap?
  • Convert Func to Func
  • Why is OpenID Connect considered mobile friendly compared to SAML
  • Custom Nav Title offset ios 11
  • setContentView() is not enough to switch between layouts?
  • Blackberry 6: how to detect a long click on track pad?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Functions in global context
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • unknown Exception android
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • failed to connect to specific WiFi in android programmatically
  • How to load view controller without button in storyboard?