Triggering shouldStartLoadWithRequest with multiple [removed].href calls

前端 未结 3 1983
清歌不尽
清歌不尽 2020-12-05 03:56

Im trying to pass multiple things from a webpage inside a UIWebView back to my iPhone app via the shouldStartLoadWithRequest method of the UIWebView.

Basically my we

3条回答
  •  时光取名叫无心
    2020-12-05 04:22

    I struck this problem also and here is my solution that works for me. All my JavaScript functions use this function __js2oc(msg) to pass data and events to Objective-C via shouldStartLoadWithRequest: P.S. replace "command:" with your "appname:" trigger you use.

    /* iPhone JS2Objective-C bridge interface */
    var __js2oc_wait = 300; // min delay between calls in milliseconds
    var __prev_t = 0;
    function __js2oc(m) {
      // It's a VERY NARROW Bridge so traffic must be throttled
      var __now = new Date();
      var __curr_t = __now.getTime();
      var __diff_t = __curr_t - __prev_t;
      if (__diff_t > __js2oc_wait) {
        __prev_t = __curr_t;
       window.location.href = "command:" + m;
      } else {
        __prev_t = __curr_t + __js2oc_wait - __diff_t;
        setTimeout( function() {
          window.location.href = "command:" + m;
        }, (__js2oc_wait - __diff_t));
      }
    }
    

提交回复
热议问题