​​​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:

  • Setup your app.
  • Initializing the SDK.
  • Adding a web view.
  • Notifying the SDK when something occurs in the web view.
  • Getting events from the SDK.

Swift Package Manager

In Xcode, navigate to FileSwift PackagesAdd Package Dependency and enter the repository URL: https://github.com/klarna/klarna-mobile-sdk-spm

In Version, Select Up to Next Major and take the default option. Then choose KlarnaMobileSDK in the Package Product column.

Cocoapods

If you’re using Cocoapods, you can add the SDK by adding the dependency to your Podfile:

pod "KlarnaMobileSDK"

Followed by performing:

pod install

You should then be able to import the KlarnaMobileSDK module in your workspace.

Carthage

If you’re using Carthage, you can add the SDK to your Cartfile as below:

 binary "https://raw.githubusercontent.com/klarna/klarna-mobile-sdk/master/KlarnaMobileSDK.json"

Followed by performing:

 carthage update --use-xcframeworks

You should be then able to add the XCFramework to your project.

To read more about Mobile SDK versioning policy, check out this section.

Return URL

Klarna purchase flows might require authorizations in other applications (e.g. bank apps) or do a handover to the Klarna app. In such cases, a return URL to your application ensures seamless return to the flow in your app, hence setting up a return URL is required. It is expected that redirects to this URL should only open your application without any changes in the UI state, ensuring the customer can continue the flow prior to external navigation.

You can set up a Return URL app scheme to your application by configuring a custom URL scheme.

Important: The return URL string passed to the SDK must include :// after the scheme name. For example, if you defined myApp as the scheme, you must use "myApp://" as the return URL argument to the SDK.

To avoid a Klarna specific app scheme, you can use a host in a common scheme for Klarna redirects, e.g. myApp://klarna-redirect , this can allow you to differentiate and handle these redirect in your handler.

Considering the return URL is a constant value in Constants.klarnaReturnUrl, you can handle redirects to your return URL as such:

Klarna App Queries

Klarna flows on mobile utilize Application Queries for Klarna app schemes to offer seamless app handover experience to customers. In order for the SDK to check availability of the Klarna app, we need you to enable querying Klarna app on the device by adding Klarna app schemes to LSApplicationQueriesSchemes.

This can be configured easily in XCode by going to your project setting and under "Info"(alternatively this is also available in your Info.plist file) you should see an entry list for Queried URL Schemes, this list should contain the klarna and klarnaconsent schemes:

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.
eventHandlerKlarnaEventHandlerCallback 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 handler 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  WKWebView, 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 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.

For your 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.

For WKWebView, you should do this in your WKNavigationDelegate:

Your app will need to implement the KlarnaEventHandler 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 two events:

  • one dispatched event.
  • one error event.

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

The SDK will log events and errors while it’s running, which you can read in XCode console. You can set the logging level for the SDK through the loggingLevel property of integration instance.

SWIFT
hybridSdk.loggingLevel = .verbose

KlarnaLoggingLevel

NameDescription
KlarnaLoggingLevel.offNothing will be logged into console.
KlarnaLoggingLevel.errorOnly errors will be logged into console.
KlarnaLoggingLevel.verboseEverything will be logged into console.