Klarna Docs - Klarna Standalone WebView

Klarna Standalone WebView

KlarnaStandaloneWebView is a new component embedded in the KlarnaMobileSDK to provide an easier integration way to enable Klarna features inside your app. It is a "WebView" with some modifications to allow Klarna to show and render content keeping the native feel of the app.

In this step-by-step guide you will learn how to integrate the KlarnaStandaloneWebView into your iOS app.

In order to display the KlarnaStandaloneWebView it needs to be instantiated using any of the available initializers with their respective parameters and then added to the view hierarchy. 

By default the size of the KlarnaStandaloneWebView is zero (0), so it is up to you to handle the final size of the view, preferably using constraints.

Important: The KlarnaStandaloneWebView can not be instantiated using the interface builder, either through storyboards or XIB files, doing so will result in a crash.

To initialize the KlarnaStandaloneWebView you can pass the following parameters through the initialisers available:

returnURLURLURL schema as defined in your app’s Plist to return from external applications.
eventHandlerKlarnaEventHandlerA listener that will receive events from the SDK.
environmentKlarnaEnvironmentInitialises the SDK with the specified Environment. For possible values check KlarnaEnvironment
regionKlarnaRegionInitializes the SDK with a specified Region. For possible values check KlarnaRegion.
// Create the view
let returnURL = URL(string: "app-schema://")
let webView = KlarnaStandaloneWebView(returnURL: returnURL, eventHandler: KlarnaEventHandler, environment: KlarnaEnvironment, region: KlarnaRegion)

// Added to the view hierarchy

// Update constraints accordingly
webView.heightAnchor.constraint(equalTo: view.height.anchor)

As previously mentioned, the KlarnaStandaloneWebView can be perceived as a specialized WKWebView developed specifically for displaying Klarna-related content. Consequently, it inherits many methods and properties from the standard WebView with the addition of some Klarna-specific methods and properties.

In essence, this means that while the KlarnaStandaloneWebView functions similarly to a typical WebView and maintains its broad functionality, it also includes certain distinct features that cater specifically to the Klarna content, enhancing its usability and performance within a Klarna context.

Since the KlarnaStandaloneWebView is an enhanced "WebView" some of the methods available are equivalent to those found in a WKWebView class. Please be aware this list is not exhaustive.

For example:

  • loadURLRequest(rq: URLRequest)
  • loadURL(url: URL)
  • loadFileURL(url: URL, allowingReadAccessTo readAccessUrl: URL)
  • loadHTML(htmlString: String, withBaseURL baseUrl: URL? = nil)
  • reload()
  • reloadFromOrigin()
  • stopLoading()
  • goBack()
  • goForward()

Adding javascript or user scripts to the KlarnaStandaloneWebView is also possible using any of the available methods for it.

For example:

  • evaluateJavaScript(javaScript: String, frame: WKFrameInfo?, completion: (((Any?, Error?) -> Void))?)
  • addUserScript(_ script: WKUserScript)

Note: Depending on the iOS version some methods could be unavailable.

For navigation requests validation or related, the KlarnaStandaloneWebView exposes a delegate which conforms to the KlarnaStandaloneWebViewDelegate protocol which is a combination of the WKNavigationDelegate and the WKUIDelegate protocols.