Getting started with the Swift SDK

Get started with the Knock Swift SDK to build in-app notification experiences.

To get started, you will need the following:

  • A Knock Account
  • A public API key for the Knock environment (which you'll use in the publishableKey)
  • An in-app feed channel with a workflow that produces in-app feed messages (optional)
  • An APNS channel with a workflow that produces push notifications (optional)

Installation

You can install the Swift SDK in a few different ways:

  • Swift Package Manager (SPM)
  • Carthage
  • Cocoapods

See here for more information on installation.

Initializing a Knock instance

To initialize the shared Knock instance, you are required to use your public API key, which is identified by the prefix pk_. Additionally, if you opt to utilize our KnockAppDelegate for comprehensive device token registration and management, you must also include your pushChannelId during the setup process of your instance.

You should do this setup as soon as you can. Preferrably within your AppDelgate.

Authenticating a user

Once you've configured the shared Knock instance with your public API key, the next step is to sign the user into Knock with their userId. We recommend you initiate the user sign-in process at the earliest point where the userId is known to you. This ensures that your application is ready to leverage Knock's features with the context of the signed-in user.

For interactions with your production Knock environment, you should enable enhanced security mode and provide a userToken to the signIn method. The userToken is a server-signed JWT that identifies the user making the request. This token is used to verify the authenticity of the user and is required when enhanced security mode is enabled.