Klarna Docs - Hybrid

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 nil it. You only need a single instance of the SDK, regardless of how many web views you have, but you can create several KlarnaHybridSDK instances.

ParamTypeDescription
returnUrlStringA URL that the SDK can use to return customers to your app.
eventListenerKlarnaHybridSDKEventListenerCallback interface that will notify you about changes in the SDK.

The SDK will notify you of events via an event listener that you’ll need to implement. It acts like a conventional delegate on iOS, however, unlike a delegate, it’s not optional.

You can read more about the event listener 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.

The SDK can handle both UIWebViews and WKWebViews, which it references under a single KlarnaWebView protocol.

ParamTypeDescription
webViewKlarnaWebViewA WebView that the SDK will track ongoingly.

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 UIWebViewDelegate or WKNavigationDelegate).

You should notify the SDK about upcoming navigations by calling the SDK’s shouldFollowNavigation() before a navigation occurs.

ParamTypeDescription
requestURLRequestThe request that your web view will perform.

If you have a UIWebView, you should do this in your UIWebViewDelegate:

If you have a WKWebView, you should do this in your WKNavigationDelegate:

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

ParamTypeDescription
webViewKlarnaWebViewThe web view that new content has loaded in.

If you have a UIWebView, you should do this in your UIWebViewDelegate:

If you have a WKWebView, you should do this in your WKNavigationDelegate:

Your app will need to implement the KlarnaHybridSDKListener interface. This will let your app be notified about relevant events that happen inside the web view that the SDK is observing. There are currently five events:

  • Four events notifying you of fullscreen transitions.
  • One error event.

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

You listen to these methods by implementing KlarnaHybridSDKEventListener in some part of the application.

The completion handler completion() should be called when you have performed any animations or changes to let the SDK know that your app is done with this step.