Klarna Docs - On-Site Messaging

On-Site Messaging

Adding On-Site Messaging is as easy as it gets, to achieve this tailor made experience for the user you just have to;

  • Request Access to On-site messaging
  • Create the Native View
  • Set Parameters
  • Call Render

If you are using a version of the SDK prior 2.3.2, to be able to integrate On-site messaging our team needs to enable the access for it. You can request access through your dedicated Delivery Manager. Please refer the Merchant ID (MID) and/or the name of your brand/company (merchant name), the countries you request access and the environment (playground or production).

The On-Site Messaging native view in iOS is called KlarnaOSMView. You can create KlarnaOSMView programmatically and add it to your view controller or custom views.

You can create the native view programmatically and place it in your application with desired layout options.

SWIFT
// Create the placement view
let osmView = KlarnaOSMView()
// Set constraints
// ...
osmView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(osmView)

Parameters used for the KlarnaOSMView are set as variables of the UIView instance. These can be identifier string values, amount or enumeration values.

ParamTypeDescription
clientIdStringOn-Site Messaging Client Identifier gathered from Klarna Merchant Portal.
placementKeyString ("credit-promotion-badge" or "top-strip-promotion-badge")Placement key identifier from Klarna Merchant Portal. At the moment the supported placement keys are "credit-promotion-badge" and "top-strip-promotion-badge"
localeStringLocale string for the placement configuration, default value is en-US.
purchaseAmountIntAmount for the placement. Set in micro units ($120.00 = 12000), used for amount based credit promotions.
environmentKlarnaOSMEnvironmentEnumerated environment value to specify merchant’s running environment. Default value is demo environment.
regionKlarnaOSMRegionEnumerated region value to specify merchant’s running market region.
themeKlarnaOSMThemeEnumerated theme value to specify how to stylize the view on light and dark configurations.
hostViewControllerUIViewControllerUIViewControllerinstance to specify parent view controller. Will be used for inner linking for informational pages. The reference will be kept as a weak reference.
delegateKlarnaOSMViewEventListenerThis delegate will be used to notify your application for changes in size of the native view.

Read more about Klarna On-Site Messaging activation and features here→

Environment enumeration will be used to define whether to use a demo placement or fetch placement configurations from either playground or production.

NameDescription
demoThis environment ignores all other parameters and shows a demo placement config containing all On-Site Messaging features.
playgroundThis environment is for placement configurations from playground Merchant Portal.
productionThis environment is for production builds of the app with placement configurations for production merchants.

Region enumeration will be used to define which endpoint to connect as a source for the placement configurations.

NameDescriptions
euRegion value for Europe.
naRegion value for North America.
ocRegion value for Oceania.

Theme enumeration will be used to define which style of OSM to be used, depending on either the app’s configuration or the system setting.

NameDescriptions
lightLight style for placement view.
darkDark style for placement view.
automaticAutomatic theme that will use the system’s user interface style.

All of the parameters for On-Site Messaging are stored as variables on the KlarnaOSMView instance. The required ones need to be set before calling the render method, otherwise the RenderResult will return a validation error for the missing parameter.

SWIFT
osmView.clientId = "<client-id>"
osmView.placementKey = "<placement-key>"
osmView.locale = "en-US"
osmView.purchaseAmount = 1000
osmView.environment = .production
osmView.region = .na
osmView.theme = .dark
osmView.delegate = self
osmView.hostViewController = self

Before rendering the OSM View and display its contents, the SDK doesn’t know how the view is integrated in the hierarchy, so it exposes a delegate (old implementation versions <=2.2.2) and a sizingDelegate (latest implementation >2.2.2). These delegates inform the integrator about the corresponding size of the OSMView after the render function is called, allowing applications to update the UI accordingly with the changes in the OSM view height.

Depending on the SDK version you need to set one or the other, not both.

SWIFT
osmView.delegate = self

This delegate conforms to the protocol KlarnaOSMViewEventListener, which specify only one function to be implemented, which is the following one:

SWIFT
func klarnaOSMViewResized(_ height: CGFloat)

That is where the calculated height of the OSM View based on the content rendered will be returned, so that your UI can be adjusted with that value.

SWIFT
osmView.sizingDelegate = self

This new delegate conforms to the protocol KlarnaSizingDelegate, which specify only one function to be implemented as well, the following one:

SWIFT
func klarnaComponent(_ klarnaComponent: KlarnaComponent, resizedToHeight height: CGFloat)

This new function will receive two parameters:

  • KlarnaComponent: Since Klarna offers different components in the SDK, if you are using more than one, it is useful to know which component changed its height.
  • Height: is the final height of the OSMView calculated when the render method is called and returned based on the contents to be displayed.

Inside this method you can adjust the UI with the proper height calculated for the OSM View.

Once you have set all the parameters to the KlarnaOSMView, you are ready to render the actual placement view. To do that, you need to call the render method of the view with the RenderResult callback parameter.

Then, once the render method is called, first the view will validate all the parameters. If there is a missing parameter the RenderResult callback will be invoked with corresponding error values. If parameters are valid then the view will try to fetch placement configuration from On-Site Messaging API. Any errors or failures from this network request will also invoke RenderResult callback if necessary. If the placement configuration is fetched and valid then the KlarnaOSMView will render it with native views and invoke the RenderResult callback with a null value.

The RenderResult callback can be used for logging purposes or to make the KlarnaOSMView visible or not, according to the error.

SWIFT
osmView.render(callback: { [weak self] error in
    ...
})

Similar to other parts of the SDK, On-Site Messaging also makes use of the KlarnaMobileSDKError class. To read more about the error handling and error object properties refer to the On-Site Messaging Error Handling guide.

These are the predefined names for errors that can happen during the placement render flow. These names are available as static String values with typealias KlarnaOSMError.

Error NameDescription
KlarnaOSMErrorMissingClientIdClient ID was not set. Set theclientIdvariable and invoke render again.
KlarnaOSMErrorMissingPlacementKeyPlacement key was not set. Set theplacementKeyvariable and invoke render again.
KlarnaOSMErrorMissingRegionRegion was not set. Set theregionvariable and invoke render again.
KlarnaOSMErrorMissingHostHosting activity was not set. Set thehostActivityvariable and invoke render again.
KlarnaOSMErrorInvalidPlacementConfigFetched configuration from the API can not be rendered by the native view.
KlarnaOSMErrorPlacementErrorOn-Site Messaging API has returned an error. Message of this error will contain information sent from the API.
KlarnaOSMErrorNetworkErrorA network error occurred and the placement will not be rendered.
KlarnaOSMErrorDisabledNative On-Site Messaging has been disabled by Klarna, the placement will not be rendered.