Search code examples
iosobjective-cwkwebviewzooming

WkWebView not allow to zoom some website after loading


Here i added programmatically my WkWebView in to main view, then after i added this delegate for WkWebView

self.wkWebView.UIDelegate = self;
self.wkWebView.navigationDelegate = self;
wkWebView.scrollView.delegate = self;

and then loading website one by one and checking zoom functionality, it's zooming for some website and not zooming for other website

If i am loading http://www.google.com - then it's allow to me for zooming and if i am loading https://www.42gears.com & https://www.facebook.com - then it's not allow to zooming functionality (This all website's zooming is working in safari, but not working in WKWebView)

Note : i am checking in iPhone and i am working with Objective-C

Please help me, Thanks in advance.


Solution

  • Here i evaluate Javascript for zooming functionality, and here you need to add this in didFinishNavigation method. so using this you can fix this issue.

    bool isAllowZoom = false;
    
    -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
    {
       if(isAllowZoom){
            NSString *javascript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=10.0, user-scalable=yes');document.getElementsByTagName('head')[0].appendChild(meta);";
           [webView evaluateJavaScript:javascript completionHandler:nil];
        }
        else
        {
            NSString *javascript = @"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:javascript completionHandler:nil];
        }
    }
    

    Thanks, may be this will help you.