forked from shripalsoni04/nativescript-webview-interface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ios.js
98 lines (88 loc) · 3.54 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var common = require("./index-common");
/**
* iOS specific WebViewInterface Class
*/
var WebViewInterface = (function(_super){
__extends(WebViewInterface, _super);
function WebViewInterface(webView, src){
_super.call(this, webView);
/**
* Since NativeScript v3.4.0 the UI component WebView is using WKWebView which broke this plugin.
* This property is used to overcome this issue and maintain compatibility with older versions of NativeScript.
*
* @see https://github.com/shripalsoni04/nativescript-webview-interface/issues/22
*
*/
if (this.webView.ios) {
this.isUsingWKWebView = this.webView.ios.constructor.name === "WKWebView";
} else {
this.isUsingWKWebView = true;
}
this._listenWebViewLoadStarted();
if(src){
this.webView.src = src;
}
}
/**
* 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 (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;
this._executeJS('window.nsWebViewInterface._getIOSResponse('+oReqMsg.resId+')')
.then(function(data) {
if(data) {
this._onWebViewEvent(eventName, data);
}
}.bind(this))
.catch(function(error) {
throw error;
});
}
}
};
/**
* Attaches loadStarted event listener on webView to intercept calls and process them.
*/
WebViewInterface.prototype._listenWebViewLoadStarted = function(){
this.webView.on('loadStarted', this._interceptCallsFromWebview, this);
}
/**
* Executes event/command/jsFunction in webView.
*/
WebViewInterface.prototype._executeJS = function(strJSFunction) {
return new Promise(function(resolve, reject) {
if (this.isUsingWKWebView) {
if (this.webView && this.webView.ios && this.webView.ios.evaluateJavaScriptCompletionHandler) {
this.webView.ios.evaluateJavaScriptCompletionHandler(strJSFunction, function(data, error) {
if (error) {
reject(error);
} else {
resolve(data);
}
});
}
} else {
resolve(this.webView.ios.stringByEvaluatingJavaScriptFromString(strJSFunction));
}
}.bind(this));
};
/**
* Removes loadStarted event listener.
*/
WebViewInterface.prototype._destroy = function () {
this.webView.off('loadStarted', this._interceptCallsFromWebview, this);
};
return WebViewInterface;
})(common.WebViewInterface);
exports.WebViewInterface = WebViewInterface;