Klarna Docs - Hybrid

You can hook up your web view to the SDK in only a few steps and immediately provide a much more pleasant experience when using Klarna products on mobile.

This guide will walk you through:

  • Initializing the SDK.
  • Adding a web view.
  • Notifyng the SDK when something occurs in the web view.
  • Getting events from the SDK.

Setting up the SDK consists of creating an instance of the SDK and implementing a listener.

Initialize the Hybrid SDK by creating a new instance of KlarnaHybridSDK. You should hold a strong reference to the SDK. It will deallocate all its resources if you null it. You only need a single instance of the SDK, regardless of how many web views you have, but if you need to, you can create several SDKs.

ParamTypeDescription
returnUrlStringA URL that the SDK can use to return customers to your app.
eventCallbackKlarnaEventCallback(Optional) Event Callback interface that will notify you about messages and errors.
fullscreenEventCallbackKlarnaFullscreenEventCallback(Optional) Fullscreen event Callback interface that will notify you about fullscreen transition events.

The SDK will notify you of events and errors via event callback object that you’ll need to implement. The SDK also notifies you about different events during fullscreen transition via fullscreen event callback.

You can read more about the callback at the end of this page.

You need to add the web views that the SDK should track. The SDK will hold weak references to these web views, so if they’re deallocated, the SDK will lose track of them.

Create an instance of WebView either in your layout or directly in your code and pass it to the SDK with addWebView(). This web view will be used to render your checkout content.

ParamTypeDescription
webViewWebViewA WebView that the SDK will track ongoingly.

To add a web view to the SDK from a layout:

To add a web view to the SDK from code:

There are two instances at which you’ll need to notify the SDK of events in your web view (as we don’t override your WebViewClient).

You should notify the SDK about upcoming navigations by calling the SDK’s shouldFollowNavigation() from your WebViewClient.

ParamParamDescription
urlURLThe URL that will be loaded.

You need to notify the SDK after a page has loaded by calling the SDK’s newPageLoad() from your WebViewClient.

ParamTypeDescription
webViewWebViewThe WebView that new content has loaded in.

KlarnaEventCallback

You will need to implement the KlarnaEventCallback interface in order to receive events and errors from the SDK. This will let your app be notified about relevant events and errors that happen inside the web view that the SDK is observing.

KOTLIN
    class MyActivity: AppCompatActivity(), KlarnaEventCallback {
        // ...
        override fun onEvent(view: View, eventName: String, params: Map<String, Any?>) {
            TODO("Not yet implemented")
        }

        override fun onErrorOccurred(view: View, error: KlarnaMobileSDKError) {
            TODO("Not yet implemented")
        }
    }

Kotlin

JAVA
    class MyActivity extends AppCompatActivity implements KlarnaEventCallback {
        // ...
        @Override
        public void onEvent(@NotNull View view, @NotNull String eventName, @NotNull Map<String, ?> params) {
            // TODO Not yet implemented
        }
    
        @Override
        public void onErrorOccurred(@NotNull View view, @NotNull KlarnaMobileSDKError error) {
            // TODO Not yet implemented

KlarnaFullscreenEventCallback

You will need to implement the KlarnaFullscreenEventCallback interface in order to receive events from the SDK during the fullscreen transition.

All of these provide you with the web view these events occurred in. You can read more about the fullscreen events here.

KOTLIN
    class MyActivity: AppCompatActivity(), KlarnaFullscreenEventCallback {
        // ...
        override fun willShowFullscreenContent(webView: WebView, completion: OnCompletion) {
            TODO("Not yet implemented")
            // ...
            completion.run()
        }
    
        override fun didShowFullscreenContent(webView: WebView, completion: OnCompletion) {
            TODO("Not yet implemented")
JAVA
    class MyActivity extends AppCompatActivity implements KlarnaFullscreenEventCallback {
        // ...
        @Override
        public void willShowFullscreenContent(@NotNull WebView webView, @NotNull OnCompletion completion) {
            // TODO Not yet implemented
            // ...
            completion.run();
        }
    
        @Override

The completion handler completion.run() should be called when you have performed any animations or changes to let the SDK know that your app is done with this step. The only exception is onErrorOccurred() which does not expect an action to be performed by the app when called.