0

I load a pdf file of size >5MB in my UIWebView. I want to scale the PDF file to certain level after loading.

    func webViewDidFinishLoad(webView: UIWebView) {
        webView.scrollView.setZoomScale(3.0, animated: false)
        webView.scrollView.contentOffset = CGPointMake(0, -(self.webView.scrollView.contentInset.top))
    }

The above snippet gives the expected result on iOS 9+. But on iOS 8, when webViewDidFinishLoad is fired, the pdf is not being loaded and there is no effect. enter image description here

I also tried

        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            webView.scrollView.setZoomScale(3.0, animated: false)
            webView.scrollView.contentOffset = CGPointMake(0, -(self.webView.scrollView.contentInset.top))
        }

It works fine, but not every time. Increasing the time is not a good idea, because, sometimes webView takes more than 5 seconds to load the file.

I tried many solutions that I got on googling

UIWebView: when did a page really finish loading?

How to detect when a UIWebView has completely finished loading?

UIWebView - How to identify the "last" webViewDidFinishLoad message?

But nothing solved my problem.

The activity indicator that we see in the screenshot is not added by me. The indicator disappears after loading the PDF completely. Anyhow if we could identify the event when indicator disappears, there I can scale the file.

Is there any way to identify it? Or any other way to identify the completion of file loading?

Community
  • 1
  • 1
iOS
  • 3,526
  • 3
  • 37
  • 82

1 Answers1

0

I had a similar problem in WKWebView. My solution was to add an observer for contentSize to the webView.scrollView. Not sure if this will help for your problem, but hopefully it will point you in the right direction.

webView.scrollView.addObserver(self, forKeyPath:  #keyPath(UIScrollView.contentSize), options: .new, context: nil)

Do something when you get the result

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath ==  #keyPath(UIScrollView.contentSize) {
        //do something
    }
}
Carien van Zyl
  • 2,853
  • 22
  • 30
  • The problem I face in adding observer is, when user does pinch zoom also, the listener is called there by ending up with page flickering. Is there a way to identify the `contentSize` is caused by user in `observeValue()`? – iOS Oct 06 '16 at 09:27