Skip to content

Latest commit

 

History

History
81 lines (59 loc) · 3.53 KB

mobile_device_support.md

File metadata and controls

81 lines (59 loc) · 3.53 KB

Integration with web views on mobile devices

The SDK can be used inside webviews on native mobile apps for iOS and Android. You will need to add some additional code to tell the SDK to update its local evaluations when the app comes to the foreground after being placed into the background. This will ensure the SDK has the latest evaluations as the SSE stream will not receive events while the app is suspended in the background. Similarly, you may want refresh the SDK if the network comes online after a period of no connectivity.

The SDK provides a function on the client instance called refreshEvaluations. Calling this allows you to soft poll the servers for the latest evaluations. Note to avoid overloading the backend servers this function will only call out to the network after enough time has elapsed. Requires version 1.14.0+ of the JS SDK.

toForeground JS function

Once you have a client instance (as shown on the previous page) add a function that can be easily invoked from the device's native language

  function toForeground() {
    client.refreshEvaluations()
  }

iOS

On iOS add an observer to wait for willEnterForegroundNotification on the WKWebView to call evaluateJavaScript()

  _ = NotificationCenter.default.addObserver(
      forName: UIApplication.willEnterForegroundNotification,
      object: nil,
      queue: .main
  ) { (notification: Notification) in

      if UIApplication.shared.applicationState == .background {

          // tell the embedded JS SDK to refresh itself
          getWebView().evaluateJavaScript("toForeground();") { (result, error) in
             ...
          }
      }
  }

Android

On Android register an ActivityLifecycleCallbacks listener and override onActivityStarted() (API level 29) then once a foreground event arrives you can call evaluateJavascript() on the WebView

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

          @Override
          public void onActivityStarted(@NonNull Activity activity) {
              WebView webView = findViewById(R.id.webview);
              // tell the embedded JS SDK to refresh itself
              webView.evaluateJavascript("toForeground();", (result) -> {
                  ...
              });
          }
      });
  }

Mobile web browsers

When not embedded inside a webview and using a mobile browser you can detect when the browser app comes to the foreground by listening for the 'visibilitychange' event as described here Page Lifecycle API

document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'visible') {
    client.refreshEvaluations();
  }
})