iOS integration

Learn how to integrate and manage Capping in your iOS applications.

This page explains how to integrate Capping with the iOS SDK.

Install the SDK

Latest version: 1.3.1. Requires iOS 14.0+ and Swift 5.10+.

To add LMDCapping in a SwiftPM project, add it to your Package.swift:

dependencies: [
  .package(url: "https://github.com/lemonde/capping-spm.git", from: "1.3.1")
]

And then add the following product to any target that needs access to the library:

targets: [
    .target(name: "MyTarget", dependencies: [.product(name: "LMDCapping", package: "capping-spm")])
]

Initialize the SDK

Create a CappingService object with the two required parameters:

  • baseURL: the base URL that will be used for the Capping operations requests.
  • apiKey: the API key that will be used for the Capping operations requests.

Store the Capping service in a property. Don’t create a new instance every time you need it, use the same instance throughout the lifecycle of the app.

Make sure to set the user ID or the tracking will not work.

import LMDCapping

let baseURL = URL(string: "https://capping.sirius.press")!
let cappingService = CappingService(baseURL: baseURL, apiKey: "<api-key>")
cappingService.setUserId("<user-id>")
self.cappingService = cappingService

The user ID can be updated at runtime. Make sure no Capping session is running when updating it.

Start and stop tracking

By default, the SDK does nothing. You must call startSession() and stopSession() in order to start and stop the tracking.

cappingService.startSession()
cappingService.stopSession()

You can either call startSession() when the app is open and stopSession() when it is closed, or you can fine-tune Capping sessions, for example by calling startSession() when an article is open and stopSession() when it is closed.

The SDK does not follow the application lifecycle: call stopSession() when the app goes to the background, and startSession() when it comes back.

Display a blocking message

You are responsible for building the UI that blocks content. Register an object to be notified of the lock status changes:

cappingService.addObserver(<observer>, selector: #selector(lockStatusDidChange(_:)))

The current status can be accessed through the lock property of the CappingService, which returns an instance of CappingLock:

@objc private func lockStatusDidChange(_ notification: Notification) {
    switch self.cappingService.lock {
        case .blocked(_):
            print("Session changed to LOCKED")
            // Block content
        case .unblocked:
            print("Session changed to UNLOCKED")
            // Unblock content
    }
}

The notification also embeds the CappingLock instance that triggered it, which is accessible with the key LockStatusDidChangeUserInfoKey inside the userInfo.

When not needed anymore, you can remove a registered observer by calling:

cappingService.removeObserver(<observer>)

Track events

In order to track events on iOS, you can use the trackEvent method.

Available events:

public enum CappingEvent: String
{
    case conversion
    case popinDisplayed
}

Example with the conversion event:

cappingService.trackEvent(.conversion)

Send popinDisplayed when you display the blocking message.

Deactivate Capping

On creation, the Capping service is activated by default. Use this property to deactivate it:

cappingService.isActive = false

Make sure to stop any running session before deactivating the Capping service.

Handle errors

Any error encountered during the Capping operations will be forwarded to the errorDelegate of the CappingService. These are only informative and no action is expected as the internal mechanics of the CappingService always handle these errors. To be notified of errors, assign the delegate, and implement the method required by the CappingServiceErrorDelegate protocol.

cappingService.errorDelegate = <delegate>
...
func  cappingServiceOperationFailedWithError(_ error: NSError) { ... }

License

This project is licensed under the software license of Société Éditrice Du Monde, Version 1.0 - see the LICENSE.md file for details.