forked from shripalsoni04/nativescript-webview-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
48 lines (42 loc) · 1.79 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var common = require("./index-common");
/**
* iOS specific WebViewInterface Class
*/
var WebViewInterface = (function(_super){
__extends(WebViewInterface, _super);
function WebViewInterface(webView){
_super.call(this, webView);
this._interceptCallsFromWebview();
}
/**
* Intercepts all requests from webView and processes requests with js2ios: protocol.
* Communication from webView to iOS is done by custom urls.
* e.g js2ios:{"eventName": "anyEvent", "resId": number}. Here resId is used as unique message id
* to fetch result from webView as we cannot rely on url for large results.
*
*/
WebViewInterface.prototype._interceptCallsFromWebview = function(){
this.webView.on('loadStarted', function (args) {
var request = args.url;
var reqMsgProtocol = 'js2ios:';
var reqMsgStartIndex = request.indexOf(reqMsgProtocol);
if (reqMsgStartIndex === 0) {
var reqMsg = decodeURIComponent(request.substring(reqMsgProtocol.length, request.length));
var oReqMsg = common.parseJSON(reqMsg);
if(oReqMsg){
var eventName = oReqMsg.eventName;
var data = this._executeJS('window.nsWebViewInterface._getIOSResponse('+oReqMsg.resId+')');
this._onWebViewEvent(eventName, data);
}
}
}.bind(this));
}
/**
* Executes event/command/jsFunction in webView.
*/
WebViewInterface.prototype._executeJS = function(strJSFunction){
return this.webView.ios.stringByEvaluatingJavaScriptFromString(strJSFunction);
};
return WebViewInterface;
})(common.WebViewInterface);
exports.WebViewInterface = WebViewInterface;