How to call Objective-C from Javascript?

前端 未结 8 1679
误落风尘
误落风尘 2020-11-22 17:38

I have a WebView, and I want to call a view in Objective-C from JavaScript. Does someone know how I can do this?


I have this code in my ViewController:

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 18:03

    The window.location method of calling objective c from JS isn't recommended. One example of problems: if you make two immediate consecutive calls one is ignored (since you can't change location too quickly) - try it yourself..

    I recommend the following alternative approach:

    function execute(url) 
    {
      var iframe = document.createElement("IFRAME");
      iframe.setAttribute("src", url);
      document.documentElement.appendChild(iframe);
      iframe.parentNode.removeChild(iframe);
      iframe = null;
    }
    

    You call the execute function repeatedly and since each call executes in its own iframe, they should not be ignored when called quickly.

    Credits to this guy.

提交回复
热议问题