0

UIWebViewDelegate methods/functions are not being called from custom web view`.

Code I've tried:

class CustomWebView: UIWebView, UIWebViewDelegate {


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.delegate = self
    }

    // Web view delegate method - not called upon web view load
    func webViewDidFinishLoad(_ webView: UIWebView) {
        print("webViewDidFinishLoad")
    }

   /*
   // tried this also
   override func didMoveToSuperview() {
    super.didMoveToSuperview()
    self.delegate = self
   }
   */
}

// View controller code

class WebControllerVC: UIViewController, UIWebViewDelegate {
   @IBOutlet weak var wvWebView: CustomWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        wvWebView.delegate = self
        wvWebView.loadHTMLString(htmlString, baseURL: nil) // htmlString is string variable with data in HTML format 
    }

}

I tried to look around following SO Posts, but can't see solution for custom web view.

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 3
    Are you sure that initializer is being called? Have you set your breakpoint? I've done something similar to this but set myself as delegate in `viewDidLoad`. Also, the delegate function `webViewDidFinishLoad` will only get called if you send your `webview` it a particular urlRequest to render, where are you doing that? – NSGangster Sep 08 '17 at 15:52
  • Just noticed that you are using UIWebView and not a UIViewController class with a webView. Try adding it to `initWithFrame` to make sure that delegate is set no matter which initializer is used. But I think your main problem is not delegate being set but the fact you are not doing any loading of html or request. – NSGangster Sep 08 '17 at 16:02
  • but where are you calling `loadRequest(_:)` or `loadHTMLString(_:)` ? You have to load web content to get your delegate to call its methods pertaining to the loading of web content – NSGangster Sep 08 '17 at 16:05
  • Can you show code for the initialization of `webView`in `ViewController` and then the loading of the html? Maybe there is something in there that needs to change. – NSGangster Sep 08 '17 at 16:07
  • I think you will have to find another method, Personally I would make the `viewcontroller` the delegate and not the `webView` itself. I just found in apple documentation they specifically say under class reference for `UIWebView` under `Subclassing Notes` not to subclass it. https://developer.apple.com/documentation/uikit/uiwebview – NSGangster Sep 08 '17 at 16:11
  • 1
    Cmon man, You set its delegate to the view controller after its initialized. So of course the delegate functions won't be called in the subclass. – NSGangster Sep 08 '17 at 16:13
  • 1
    delete line `wvWebView.delegate = self` from viewcontroller and it should work – NSGangster Sep 08 '17 at 16:14
  • Ohh I see the mistake.... – Krunal Sep 08 '17 at 16:15
  • 1
    yup. Webview can't have two delegates, and you set it to the viewcontroller which doesn't (i assume) implement any of the delegate functions. – NSGangster Sep 08 '17 at 16:16

0 Answers0