How to disable double tap and pinch zoom in/out on WKWebview in iOS Objective-C?

Viewed 577

I have face problem to disable zoom in/out on WKWebView. I get many solution for that, but that all solution not work in all case.

Solution 1: self.webViewer.scrollView.pinchGestureRecognizer.enabled = true/false

Issue: Can not stop double tap zoom.

Solution 2:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return nil;
}

Issue: Not working on iOS 13.

Solution 3: By setting java script

NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'); document.getElementsByTagName('head')[0].appendChild(meta);";

3rd Solution is best compare to all other and it's also work fine in almost all case.

But in my case i have .pdf extension url and for that url javascript not properly work. And i cann't disable zoom in/out for that url.

URL : http://www.pdf995.com/samples/pdf.pdf

When i evaluateJavaScript using below code.

   NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 
   'viewport'); meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum- 
   scale=1.0, user-scalable=no'); document.getElementsByTagName('head')[0].appendChild(meta);";
  
  [webView evaluateJavaScript:jScript completionHandler:^(NSString *result, NSError *error)
  {
      NSLog(@"Error %@",error);
      NSLog(@"Result %@",result);
  }];

I got below error

Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=TypeError: undefined is not an object (evaluating 'document.getElementsByTagName('head')[0].appendChild'), WKJavaScriptExceptionColumnNumber=228, WKJavaScriptExceptionSourceURL=http://www.pdf995.com/samples/pdf.pdf, NSLocalizedDescription=A JavaScript exception occurred}

So i want to solution about javascript error.

Please help me.

0 Answers
Related