Hybrid交互方式

Hybrid交互方式

一.APP开发模式(这里的APP是Application的意思).

模式 Native App Web App Hybrid App ReactNative、Weex、Ionic、Flutter
优点 体验好 体验差 体验中 体验好,开发成本低
缺点 开发和发布成本高 开发和发布成本低 开发和发布成本中,初次加载或更新有白屏效果 对开发人员技术要求高

其中Hybrid App又分为两种:

1.以H5为主,Native为辅。例如Cordova,Native的作用单纯就是提供一个APP外壳和暴露App独有功能给H5。

2.以Native为主,H5为辅。

二.Hybrid的交互方式.

1.JS 调用 Native.

(1)url scheme.

1.H5将参数通过url发送给Native,并将回调函数保留在Window对象上. 2.Native通过拦截url获取H5的参数,Native进行相应处理后将调用Window对象上的回调函数.

Android的webview提供了shouldOverrideUrlLoading拦截url scheme.

IOS的webview本身会直接拦截url scheme.

(2)重写prompt、alert、confirm、console.log等方法.

1.H5通过prompt等方法发送参数 2.Native端重写prompt等方法获取参数

Android的webview的onJsAlert、onJsConfirm、onConsoleMessage、onJsPrompt可以分别拦截

js的alert、confirm、console.log、prompt方法.

iOS由于安全限制会拦截对prompt、alert、confirm、console.log,只需要实现相应的代理方法即可.

(3)注入API.

1.Native对webview的window对象注入对象或方法,H5直接调用.

Android的webview提供了addJavascriptInterface方法.

IOS的webview提供了JavascriptCore方法.

2.Native调用JS.

H5将回调函数注册在window对象下,native直接调用. Android和IOS都提供了evaluateJavaScript来执行js代码.

三.例子:Jockey.js的H5实现(url scheme方式).

Jockey.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// JockeyJS
//
// Copyright (c) 2013, Tim Coulter
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/* eslint-disable */
;(function () {
if (window.Jockey) return
// Non-accessible variable to send to the app, to ensure events only
// come from the desired host.
var host = window.location.host;

var Dispatcher = {
callbacks: {},

send: function(envelope, complete) {
var dispatcher = this;

this.callbacks[envelope.id] = function() {
complete.apply(this, arguments);

delete dispatcher.callbacks[envelope.id];
};

this.dispatchMessage("event", envelope);
},

sendCallback: function(messageId) {
var envelope = Jockey.createEnvelope(messageId);

this.dispatchMessage("callback", envelope);
},

triggerCallback: function(id) {
var dispatcher = this;

var args = Array.prototype.slice.call(arguments);
args.shift();


// Alerts within JS callbacks will sometimes freeze the iOS app.
// Let's wrap the callback in a timeout to prevent this.
setTimeout(function() {
var callback = dispatcher.callbacks[id];
callback.apply(this, args);
}, 0);
},

// `type` can either be "event" or "callback"
dispatchMessage: function(type, envelope) {
// We send the message by navigating the browser to a special URL.
// The iOS library will catch the navigation, prevent the UIWebView
// from continuing, and use the data in the URL to execute code
// within the iOS app.

var src = "jockey://" + type + "/" + envelope.id + "?" + encodeURIComponent(JSON.stringify(envelope));
var iframe = document.createElement("iframe");
iframe.setAttribute("src", src);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
};

var Jockey = {
listeners: {},

dispatcher: null,

messageCount: 0,

on: function(type, fn) {
if (!this.listeners.hasOwnProperty(type) || !this.listeners[type] instanceof Array) {
this.listeners[type] = [];
}

this.listeners[type].push(fn);
},

off: function(type) {
if (!this.listeners.hasOwnProperty(type) || !this.listeners[type] instanceof Array) {
this.listeners[type] = [];
}

this.listeners[type] = [];
},

send: function(type, payload, complete) {
if (payload instanceof Function) {
complete = payload;
payload = null;
}

payload = payload || {};
complete = complete || function() {};

var envelope = this.createEnvelope(this.messageCount, type, payload);

this.dispatcher.send(envelope, complete);

this.messageCount += 1;
},

// Called by the native application when events are sent to JS from the app.
// Will execute every function, FIFO order, that was attached to this event type.
trigger: function(type, messageId, json) {
var self = this;

var listenerList = this.listeners[type] || [];

var executedCount = 0;

var complete = function() {
executedCount += 1;

if (executedCount >= listenerList.length) {
self.dispatcher.sendCallback(messageId);
}
};

for (var index = 0; index < listenerList.length; index++) {
var listener = listenerList[index];

// If it's a "sync" listener, we'll call the complete() function
// after it has finished. If it's async, we expect it to call complete().
if (listener.length <= 1) {
listener(json);
complete();
} else {
listener(json, complete);
}
}

},

// Called by the native application in response to an event sent to it.
// This will trigger the callback passed to the send() function for
// a given message.
triggerCallback: function(id) {
this.dispatcher.triggerCallback.apply(this.dispatcher, arguments);
},

createEnvelope: function(id, type, payload) {
return {
id: id,
type: type,
host: host,
payload: payload
};
}
};

// i.e., on a Desktop browser.
var nullDispatcher = {
send: function(envelope, complete) { complete(); },
triggerCallback: function() {},
sendCallback: function() {}
};

// Dispatcher detection. Currently only supports iOS.
// Looking for equivalent Android implementation.
var i = 0,
j = 0,
iOS = false,
isPC = false,
pcDevice = ['Win', 'Mac'],
iDevice = ['iPad', 'iPhone', 'iPod'];

for (; i < iDevice.length; i++) {
if (navigator.platform.indexOf(iDevice[i]) >= 0) {
iOS = true;
break;
}
}

for (; j < pcDevice.length; j++) {
if (navigator.platform.indexOf(pcDevice[j]) >= 0) {
isPC = true;
break;
}
}

// Detect UIWebview. In Mobile Safari proper, jockey urls cause a popup to
// be shown that says "Safari cannot open page because the URL is invalid."
// From here: http://stackoverflow.com/questions/4460205/detect-ipad-iphone-webview-via-javascript

var UIWebView = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
var isAndroid = navigator.userAgent.toLowerCase().indexOf("android") > -1;

if ((iOS && UIWebView) || (isAndroid && !isPC)) {
Jockey.dispatcher = Dispatcher;
} else {
Jockey.dispatcher = nullDispatcher;
}

window.Jockey = Jockey;
})();

export default Jockey;