Klarna Docs - Sign in with Klarna
Sign in with Klarna

In this step-by-step guide you will learn how to integrate Sign in with Klarna into your iOS app.

ASWebAuthenticationPresentationContextProviding

The iOS integration will render the sign in flow in a sandboxed in-app browser through Apple's own ASWebAuthenticationSession.

SWIFT
@available(iOS 13.0, *)
extension SignInButtonView: ASWebAuthenticationPresentationContextProviding {
    /// Needed so ASWebAuthenticationSession knows what window to put the view into.
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return window
    }
}

The SDK does all the work on your behalf with the parameters and keys that you supply, but you need to implement support for ASWebAuthenticationContextProviding in one of your classes and forward an instance of it to the SDK.

The UIWindow that your instance supplies will be used to present the in-app browser.

Event Handler

While the user is interacting with the Sign in with Klarna flow or when the flow is completed, you'll receive events by setting an event handler. You can see a sample event handler on the right.

SWIFT
@available(iOS 13.0, *)
extension SignInViewController: KlarnaEventHandler {
    func klarnaComponent(_ klarnaComponent: KlarnaComponent, dispatchedEvent event: KlarnaProductEvent) {
         if event.action == .klarnaSignInToken {
            // Authorization went through
            let token = event.params["klarnaToken"]
        } else if event.action == .klarnaSignInUserTappedButton {
            // Button was tapped
        } else if event.action == .klarnaSignInUserCancelled {
            // User cancelled out of the flow

If you choose to use universal links, you'll need to configure your app or scene delegate to support forwarding the universal link to our component. 

Please follow this section if you would like to use one of the button designs provided by Klarna.

1. Create the Button 

You can create an instance of KlarnaSignInButton using its default initializer.

SWIFT
let signInButton = KlarnaSignInButton(clientId: clientID,
                                  scope: scope,
                                  market: market,
                                  locale: locale,
                                  presentationContext: self,
                                  theme: klarnaTheme,
                                  environment: env,
                                  region: reg,
                                  returnUrl: URL(string: "https://sneakers.com")!,
                                  eventHandler: self)

Parameters

When initializing the button, specify the following parameters:

ParameterRequiredDescription
clientIdYesThis is the UUID you get when creating your Klarna OAuth 2.0 app.
scopeYesSpace-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation. The claims of the requested scopes will be returned as part of JWT token id_token. Note: openid is always requested by default even if you don't pass it.Available scopes:
  • offline_access
  • profile:email
  • profile:phone
  • profile:name
  • profile:date_of_birth
  • profile:billing_address
  • profile:national_identification
  • profile:country
  • payment:request:create
marketYesThe market or the country where this integration is available, for example, SE for Sweden.
localeYesThe language in which the Sign in with Klarna button is displayed to the user.
presentationContextYesRead more about context presentation.
themeNoDefines the theming for the Sign in with Klarna UI, but not the button.
environmentNoConfigures the endpoints and other behaviors that the SDK will be operating with. When set to production, the SDK will make requests to production endpoints and perform real validation, whereas for other environments will not.
regionNoDefines the regional API endpoints to which the SDK will send requests.  
resourceEndpointNoDefines the cloud provider to which the SDK will send requests. This should not be changed or overridden.
returnUrlYesReturn URL to your application if a third-party application or the Klarna app needs to be opened. You can find more information about the URL in our In app guide.
eventHandlerYesThe event handler for errors and events as described in the Preparation section.

You can add the button to your app's view hierarchy with addSubview(). The button's contents will self-size according to the available space and the user's dynamic type accessibility settings.

When the user taps the button, the sign in flow will be triggered without any additional work from your side. When the flow completes, one of the two methods in your event handler will be called. 

When the authentication flow is complete, the klarnaComponent(_:dispatchedEvent:) method of your event handler will be called.

Please follow this section if you want to add a button with a custom design.

1. Create an Instance of the SDK

To integrate the Sign in with Klarna SDK in your iOS app, you need to create an instance of KlarnaSignInSDK, call its signIn() function, and receive the results once the flow is completed.

SWIFT
let sdk = KlarnaSignInSDK(
            returnUrl: URL(string: "app-schema://"),
            eventHandler: self)

To create an instance of the SDK you need to specify the following parameters.

ParameterRequiredDescription
themeNoDefines the theming for the Sign in with Klarna UI, but not the button.
environmentNoConfigures the endpoints and other behaviors that the SDK will be operating with. When set to production, the SDK will make requests to production endpoints and perform real validation, whereas for other environments will not.
regionNoDefines the regional API endpoints to which the SDK will send requests.  
resourceEndpointNoDefines the cloud provider to which the SDK will send requests. This should not be changed or overridden.
returnUrlNoReturn URL to your application if a third-party application or the Klarna app needs to be opened. You can find more information about the URL in our In app guide.
eventHandlerNoThe event handler for errors and events as described in the Preparation section.

Create and render your own button. The design and implementation are up to you.

3. Call the signIn() function

When the user chooses to sign in with Klarna, call the SDK's signIn() function. This will render the authentication flow. When the flow completes, one of the two methods in your event handler will be called.

SWIFT
    @objc func myButtonTapped() {
        self.sdk.signIn(
            clientId: "clientID",
            scope: "scope",
            market: "market",
            locale: "locale",
            presentationContext: self)
    }

The parameters that you'll need to pass to signIn() function are the following:

ParameterDescription
clientIdThe UUID you get when creating your Klarna OAuth 2.0 app.
scopeA space-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation. The claims of the requested scopes will be returned as part of JWT token id_token. Note: openid is always requested by default even if you don't pass it.Available scopes:
  • offline_access
  • profile:email
  • profile:phone
  • profile:name
  • profile:date_of_birth
  • profile:billing_address
  • profile:national_identification
  • profile:country
  • payment:request:create
marketThe market or the country where this integration is available, for example, SE for Sweden.
localeThe language in which the Sign in with Klarna button is displayed to the user.
presentationContextRead more about context presentation.

When the authentication flow is complete, the klarnaComponent(_:dispatchedEvent:) method of your event handler will be called.