Protocol interception A custom protocol request is initiated by h5. After the app intercepts the request, the app calls the callback function in h5
-
The protocol is customized by the app, such as sdk://action?params
-
Define the callback function in h5, such as
window.bridge={getDouble:value=>{},getTriple:value=>{}}
-
A custom protocol request is initiated by h5, such as location.href='sdk://double?value=10'
-
After the app intercepts the request, it performs the corresponding operation to obtain the return value
-
Call the callback function in h5 by the app, such as window.bridge.getDouble(20);
javascript
window.bridge = { getDouble: value => { // 20 }, getTriple: value => { // more } }; location.href = 'sdk://double?value=10' ;
android:
webview.setWebViewClient (new new WebViewClient () { @Override public Boolean houldOverrideUrlLoading (the WebView View, String url) { // If the url is determined sdk: // starts off on the knockdown // url and from the SDK: // ? action taken action in the params and the params Uri Uri.parse = URI (URL); IF (. uri.getScheme () the equals ( "SDK")) { // = Double such action, the params = value = 10 Webview. evaluateJavascript ( 'window.bridge.getDouble (20 is)'); return to true; } } });
ios:
(BOOL) Webview :( UIWebView *) Webview shouldStartLoadWithRequest :( NSURLRequest *) Request navigationType :( UIWebViewNavigationType) navigationType { // determine if the url is sdk: // starts off on the interception // then the url sdk: // ? action taken action in the params and the params NSString * urlStr = request.URL.absoluteString; IF ([urlStr hasPrefix: @ "SDK: //"]) { // such action = double, params = value = 10 * @ FUNC = NSString "window.bridge.getDouble (20 is)"; [Webview stringByEvaluatingJavaScriptFromString: FUNC]; return NO; } return YES; }