UIWebView---OVGap

Reference:http://blog.csdn.net/woaifen3344/article/details/42742893



使用這個庫前,需要給HTML5中引入對方的腳本,叫ovgap.js,可到Github下載:

[javascript] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. ;(function() {  
  2.   
  3. var require, define;  
  4.   
  5. (function () {  
  6.     var modules = {},  
  7.     // Stack of moduleIds currently being built.  
  8.         requireStack = [],  
  9.     // Map of module ID -> index into requireStack of modules currently being built.  
  10.         inProgressModules = {},  
  11.         SEPERATOR = ".";  
  12.   
  13.     function build(module) {  
  14.         var factory = module.factory,  
  15.             localRequire = function (id) {  
  16.                 var resultantId = id;  
  17.                 //Its a relative path, so lop off the last portion and add the id (minus "./")  
  18.                 if (id.charAt(0) === ".") {  
  19.                     resultantId = module.id.slice(0, module.id.lastIndexOf(SEPERATOR)) + SEPERATOR + id.slice(2);  
  20.                 }  
  21.                 return require(resultantId);  
  22.             };  
  23.         module.exports = {};  
  24.         delete module.factory;  
  25.         factory(localRequire, module.exports, module);  
  26.         return module.exports;  
  27.     }  
  28.   
  29.     require = function (id) {  
  30.         if (!modules[id]) {  
  31.             throw "module " + id + " not found";  
  32.         } else if (id in inProgressModules) {  
  33.             var cycle = requireStack.slice(inProgressModules[id]).join('->') + '->' + id;  
  34.             throw "Cycle in require graph: " + cycle;  
  35.         }  
  36.         if (modules[id].factory) {  
  37.             try {  
  38.                 inProgressModules[id] = requireStack.length;  
  39.                 requireStack.push(id);  
  40.                 return build(modules[id]);  
  41.             } finally {  
  42.                 delete inProgressModules[id];  
  43.                 requireStack.pop();  
  44.             }  
  45.         }  
  46.         return modules[id].exports;  
  47.     };  
  48.   
  49.     define = function (id, factory) {  
  50.         if (modules[id]) {  
  51.             throw "module " + id + " already defined";  
  52.         }  
  53.   
  54.         modules[id] = {  
  55.             id: id,  
  56.             factory: factory  
  57.         };  
  58.     };  
  59.   
  60.     define.remove = function (id) {  
  61.         delete modules[id];  
  62.     };  
  63.   
  64.     define.moduleMap = modules;  
  65. })();  
  66.   
  67.   
  68.   
  69. define("ov_gap"function(require, exports, module) {  
  70.   
  71. var ovGap = {  
  72.     callbackId: Math.floor(Math.random() * 2000000000),  
  73.     callbacks: {},  
  74.     commandQueue: [],  
  75.     groupId: Math.floor(Math.random() * 300),  
  76.     groups: {},  
  77.     listeners: {},  
  78.     invoke: function(cmd, params, onSuccess, onFail) {  
  79.         if(!cmd) cmd = "defaultCommand";  
  80.         if(!params) params = {};  
  81.         this.callbackId ++;  
  82.         this.callbacks[this.callbackId] = {  
  83.             success: onSuccess,  
  84.             fail: onFail  
  85.         };  
  86.         var rurl = "ovgap://" + cmd + "/" + JSON.stringify(params) + "/" + this.callbackId;  
  87.         document.location = rurl;  
  88.     },   
  89.     dispatchCommand: function(cmd, params, onSuccess, onFail) {  
  90.         if(!cmd) cmd = "defaultCommand";  
  91.         if(!params) params = {};  
  92.         this.callbackId ++;  
  93.         this.callbacks[this.callbackId] = {  
  94.             success: onSuccess,  
  95.             fail: onFail  
  96.         };  
  97.         var command = cmd + "/" + JSON.stringify(params) + "/" + this.callbackId;  
  98.         this.commandQueue.push(command);  
  99.     },  
  100.     fetchNativeCommands: function() {  
  101.         var json = JSON.stringify(this.commandQueue);  
  102.         this.commandQueue = [];  
  103.         return json;  
  104.     },  
  105.     activate: function() {  
  106.         document.location = "ovgap://ready";  
  107.     },  
  108.     // return group ID  
  109.     createGroup: function() {  
  110.         this.groupId ++;  
  111.         this.groups[this.groupId] = [];  
  112.         return this.groupId;  
  113.     },  
  114.     dispatchCommandInGroup: function(cmd, params, onSuccess, onFail, groupId) {  
  115.         if (!this.groups[groupId]) return false;  
  116.   
  117.         if(!cmd) cmd = "defaultCommand";  
  118.         if(!params) params = {};  
  119.         this.callbackId ++;  
  120.         this.callbacks[this.callbackId] = {  
  121.             success: onSuccess,  
  122.             fail: onFail  
  123.         };  
  124.         var command = cmd + "/" + JSON.stringify(params) + "/" + this.callbackId;  
  125.         this.groups[groupId].push(command);  
  126.         return true;  
  127.     },  
  128.     activateGroup: function(groupId) {  
  129.         if (!this.groups[groupId]) return false;  
  130.         document.location = "ovgap://group/" + groupId;  
  131.     },  
  132.     fetchNativeGroupCommands: function(groupId) {  
  133.         if (!this.groups[groupId]) return [];  
  134.         var json = JSON.stringify(this.groups[groupId]);  
  135.         this.groups[groupId] = [];  
  136.         return json;  
  137.     },  
  138.     callbackSuccess: function(callbackId, params) {  
  139.         try {  
  140.             ovGap.callbackFromNative(callbackId, params, true);  
  141.         } catch (e) {  
  142.             console.log("Error in error callback: " + callbackId + " = " + e);  
  143.         }  
  144.     },  
  145.     callbackError: function(callbackId, params) {  
  146.         try {  
  147.             ovGap.callbackFromNative(callbackId, params, false);  
  148.         } catch (e) {  
  149.             console.log("Error in error callback: " + callbackId + " = " + e);  
  150.         }   
  151.     },   
  152.     callbackFromNative: function(callbackId, params, isSuccess) {  
  153.         var callback = this.callbacks[callbackId];  
  154.         if (callback) {  
  155.             if (isSuccess) {  
  156.                 callback.success && callback.success(callbackId, params);  
  157.             } else {  
  158.                 callback.fail && callback.fail(callbackId, params);  
  159.             }  
  160.             delete ovGap.callbacks[callbackId];  
  161.         };  
  162.     },  
  163.     addGapListener: function(listenId, onSuccess, onFail) {  
  164.         if (!listenId || !onSuccess || !onFail) return;  
  165.         this.listeners[listenId] = {  
  166.             success : onSuccess,   
  167.             fail : onFail  
  168.         };  
  169.     },  
  170.     removeListener: function(listenId) {  
  171.         if (!this.listeners[listenId]) return;  
  172.         this.listeners[listenId] = null;  
  173.     },  
  174.     triggerListenerSuccess: function(listenId, params) {  
  175.         if (!this.listeners[listenId]) return;  
  176.         var listener = this.listeners[listenId];  
  177.         listener.success && listener.success(listenId, params);  
  178.     },  
  179.     triggerListenerFail: function(listenId, params) {  
  180.         if (!this.listeners[listenId]) return;  
  181.         var listener = this.listeners[listenId];  
  182.         listener.fail && listener.fail(listenId, params);  
  183.     }  
  184. };  
  185.   
  186. module.exports = ovGap;  
  187.   
  188. });  
  189.   
  190. window.ov_gap = require("ov_gap");  
  191.   
  192. }) ();  

給按鈕添加一個點擊事件,回調如下:

[javascript] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. function onButtonClick() {  
  2.  // 下面是我需要處理的事,處理完之後  
  3.  alert('這裏我是要處理一些事的,如果有需要的話。');  
  4.    
  5.  // 這裏是回調我們前端與後端商量好的方法  
  6.  // activityList是oc中的方法  
  7.   window.ov_gap.invoke("activityList"null, success, fail);  
  8. }  

這樣就從JS回調到了IOS端的OC方法,然後處理我們想做的事。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章