Support Home > Reference Information > SDK Utilization from a WebView

SDK Utilization from a WebView

The following document demonstrates how to call methods and interact with the Kochava SDK from within a native WebView. While this interaction can be accomplish a number of ways, provided below is an example which can be modified based upon your needs.

 

  • WebView Javascript (HTML)

    Define any desired functions within your WebView’s HTML. In the following example, the functions which mirror the most common SDK usage are defined. Place this Javascript code within your WebView HTML.


    Android Implementation

    Add the native code which interacts with the WebView. In the code of your app attach the custom WebViewClient to your WebView instance in order to intercept and process the URLs generated by the Javascript functions.

     

    Sample Java Code:

     

    Sample Kotlin Code:


    Usage

    Now that the javascript functions have been defined and the native code within has been added within the app, call the javascript functions from within the WebView which will in turn call the corresponding methods from the SDK.

     

    Example Code:

  • WebView Javascript (HTML)

    Define any desired functions within your WebView’s HTML. In the following example, the functions which mirror the most common SDK usage are defined. Place this Javascript code within your WebView HTML.


    Apple iOS, tvOS Implementation

    Add the native code which interacts with the WebView. In order to intercept and process actions from the WebView, use the following example approach. In your UIViewController, conform to protocol WKNavigationDelegate, and implement the callback for the WKNavigationDelegate instance method webView(_: WKWebView, decidePolicyFor: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) as follows:

     

    Sample Swift Code:


    Usage

    Now that the javascript functions have been defined and the native code within has been added within the app, call the javascript functions from within the WebView which will in turn call the corresponding methods from the SDK.

     

    Example Code:

 
 

Last Modified: Nov 16, 2022 at 12:50 pm