button in uiwebview call second view native code

<html>
    <head>
        <script type='text/javascript'>
        function skipButtonTapped()
        {


     }
         
          var pinID = 0;
          function ping() {
            var message = 'hello ' + Date.now();
            document.title = message;
            window.location.hash = pinID++;
            return false;
          }
        </script>
   
    </head>
    <body style='overflow-x:hidden;overflow-y:hidden;'>
        <h2 style = "font-size:16px;" align='center'>Hi Welcome to Webpage</h2>
        <br/>


            <input type='button' style='width:90px;height:30px;' onClick="window.location.hash = ping()" value='click me'>
        </center>
       
         
    </body>
</html>

i have this page iam already show it in uiwebview ios app and i want user onclick or javascript function to go into another view controller for native code ...

Replies

This is much easier if you use WKWebView rather than UIWebView because WKWebView has much better support for web/native integration. Specifically, in WKWebView you can add a script message handler (WKScriptMessageHandler) that you can invoke directly from JavaScript. A good place to start here is this doc.

Unless you have a hard requirement to stick with the older UIWebView, I recommend you make the jump to WKWebView, which will help with this and many other issues.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

but its possible to call native app or second view controller in wkwebview ??


thx for your support

but its possible to call native app or second view controller in wkwebview ??

I wrote:

  • WKWebView has much better support for web/native integration

  • in WKWebView you can add a script message handler … that you can invoke directly from JavaScript

Is that unclear? Perhaps you can explain why bit you’re having problems with.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks You ,


I will try to use WKWEBVIEW ... any sample code or tutorial can explain more about script message handler more than apple community it would be great . and again thanks for your great support .

any sample code or tutorial can explain more about script message handler

My go-to resource for this stuff is WWDC 2016 Session 206 Introducing the Modern WebKit API.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"