Monitor subscription status

You can monitor the subscription status of the Setapp user who uses your iOS app with the help of the SetappSubscription object. Three monitoring options are available for you:

  • SetappManager delegate
  • notifications, and
  • Key-Value Observation (KVO)

📘

Note

Configure subscription status monitoring for the iOS apps only. Setapp Launch agent controls macOS app's run, so no needs to monitor subscription status for macOS apps in a different way.

Delegate

Declare a class conforming to the SetappManagerDelegate protocol and set up a delegate property for the shared instance of the SetappManager class.

import Setapp
class SetappSubscriptionManagerDelegate: SetappManagerDelegate {
  init() {
    SetappManager.shared.delegate = self
  }
  // MARK: SetappManagerDelegate
  func setappManager(
    _ manager: SetappManager,
    didUpdateSubscriptionTo newSetappSubscription: SetappSubscription
  )
  {
    print("Manager:", manager)
    print("Setapp subscription:", newSetappSubscription)
  }
}

Notification

In addition to the Delegate method, you can observe the SetappManager.didChangeSubscriptionNotification notification for the shared instance of the SetappManager object. As you can see from the example below, the manager is the object, and a new Setapp subscription state is located in the NSKeyValueChangeKey.newKey key in the userInfo property of the notification.

import Setapp
class SetappSubscriptionNotificationObserver {
  private var notificationObserver: NSObjectProtocol?
  init() {
    notificationObserver = NotificationCenter.default
      .addObserver(forName: SetappManager.didChangeSubscriptionNotification,
                   object: SetappManager.shared,
                   queue: .none) { [weak self] (notification) in
                    self?.setappSubscriptionDidChange(notification: notification)
    }
  }
  deinit {
    notificationObserver.map(NotificationCenter.default.removeObserver(_:))
  }
  // MARK: Notification
  func setappSubscriptionDidChange(notification: Notification) {
    guard
      let manager = notification.object as? SetappManager,
      let newValue = notification.userInfo?[NSKeyValueChangeKey.newKey],
      let newSetappSubscription = newValue as? SetappSubscription else {
        return
    }
    print("Manager:", manager)
    print("Setapp subscription:", newSetappSubscription)
  }
}

Key-Value Observation (KVO)

If you prefer KVO, you can observe the subscription property of the shared instance of the SetappManager class.

import Setapp
class SetappSubscriptionKVOObserver {
  private var kvoObserver: NSObjectProtocol?
  init() {
    kvoObserver = SetappManager.shared
      .observe(\.subscription, options: [.new]) { [weak self] (manager, change) in
        self?.setappSubscriptionDidChange(manager: manager, change: change)
    }
  }
  // MARK: KVO observation
  func setappSubscriptionDidChange(
    manager: SetappManager,
    change: NSKeyValueObservedChange<SetappSubscription>
  )
  {
    guard let newSetappSubscription = change.newValue else {
      return
    }
    print("Manager:", manager)
    print("Setapp subscription:", newSetappSubscription)
  }
}