The H5 communicates with Android /IOS
Android /ios communicates to H5
Ios connection introduces the H5 page, which is a user application page. To enter the page, you need to obtain the user’s userId and token and then adjust the interface to determine whether the user is qualified for application. To enter the page, you need to get url splicing parameters. The front end is captured in JS. Url concatenation parameters (for active pages, application pages…)
// Intercepts the parameters in the URL
function getUrlValue(kValue) {
var url = window.location.href; // Address of the current page
var reg = /([^?&=]+)=([^?&=]+)/g, obj = {}, str = url;
url.replace(reg, function () {
obj[arguments[1]] = arguments[2]; }) for (var keyName in obj) { if (keyName == kValue) { return obj[keyName]; // break; } } } let userId = getUrlValue('userId'); let token = getUrlValue('token'); Copy the code
H5 communicates to Android /ios
After some interfaces are requested, the interface returns an invalid or expired token or the token is successfully applied for. In this case, H5 needs to send the information returned by the interface to Android or ios
// Interact with Android or IOS
function signalCommunication(isMsg, content) {MSG =true for android /IOS, MSG =false for IOS
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > - 1 || u.indexOf('Linux') > - 1; //g
letisIOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/); / / ios terminal
if (isAndroid) { // Call the native app function if (isMsg) {/ / the message window.android.clickOnAndroid(content); } else {// Pass an error code window.android.onRequestError(content); } } if (isIOS) { if (isMsg) {/ / the message window.webkit.messageHandlers.goBackVC.postMessage({ 'goBackVC': 'fromHallMaster'. }); } else {// Pass an error code window.webkit.messageHandlers.requestErrorAction.postMessage({ 'errCode': content.errCode, 'errMsg': content.errMsg }); } } } Copy the code
This article is formatted using MDNICE