SDK Integration

How to setup the Apple Nami SDK

Please make sure you have completed the setup of your app on the Nami Control Center for all these steps to be successful.

Adding Nami to your app has a few steps for a basic app.

  1. Add the SDK to your project

  2. Configure the SDK

  3. Show a paywall in your app

  4. React to a purchase to grant access to paid content or features

We'll run through each of these below.

Nami works with SwiftUI apps! But there are a few small differences in a SwiftUI setup. Please look for additional callouts below and separate code samples for SwiftUI implementation of the Nami Platform.

Add the SDK to your project

Requirements

The Nami Apple SDK supports

  • iOS 14+

  • iPadOS 14+

  • tvOS 15+

  • Xcode 12+

Our Apple SDK is available on GitHub, with sample applications and our Nami Apple SDK.

The best way to add Nami to your project is to use the Swift Package Manager (SPM) to install the latest SDK version into your application.

We also support CocoaPods and you can manually install the XCFramework from our GitHub repository.

SPM using Xcode on the Mac:

  1. In the Xcode menu select File > Add Packages.

  2. In the right-hand corner of the modal in the box labelled 'Search or Enter Package URL', enter the URL https://github.com/namiml/NamiSDK-SwiftPackageManager.

  3. In the box below, select Dependency Rule: 'Branch' and enter the current SPM version name

  1. Select "Add Package"

  2. Verify the SPM package is loaded by looking at the project Navigator

Manual Setup

  1. Download the Nami SDK from GitHub at https://github.com/namiml/nami-apple

  2. Copy NamiApple.xcframework from the downloaded GitHub repository into the same directory as your application .xcproject file.

  3. Open your project in Xcode, select the application target in the project navigator.

  4. Select the General tab if it is not already selected.

  1. Drag the NamiApple.framework in the same directory as your .xcproject file from Finder into the project Frameworks, libraries and Embedded Content section of the General tab. Make sure Embed and Sign is selected.

  1. After this, the NamiApple framework is installed.

  2. To update, download new versions of the NamiApple.framework from the GitHub repository, and copy them on top of the Nami.framework in your application - always opt to replace the entire directory when copying.

  3. You may wish to optionally check the Nami.framework into your application repository if you are using source control.

Cocoapods

Nami is available via Cocoapods.

We recommend you switch to the Swift Package Manager as the best way to integrate the Nami framework into your app.

Configure the SDK

We recommend that you configure the Nami SDK as early in your app's launch as possible. This will ensure the SDK is ready to receive and process purchases.

For Swift, the best spot to do this is in the ApplicationDidFinishLaunchingWithOptions method in the AppDelegate.swift.

For SwiftUI, we recommend adding an init() method if not already present, to your App subclass that starts your application, usually in a file called YourAppNameApp.swift file. We also recommend that you encapsulate the Nami configuration in its own method.

For Objective-C, the best spot to do this is indidFinishLaunchingWithOptions method in the AppDelegate.m.

Here's a full code example:

import NamiApple

class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let namiConfig : NamiConfiguration = NamiConfiguration( appPlatformId: "YOUR_APP_PLATFORM_ID" )
    namiConfig.logLevel = .warn
    Nami.configure(with: namiConfig)
  }
}

You'll need to go find your App Platform ID in the Control Center > Integrations to configure the SDK.

Nami recommends setting the log level to .warn for apps published to the App Store. .info may be helpful during development to better understand what is going on. .debug level has a lot of information and is likely only helpful to the Nami support team.

SwiftUI NamiDataSource

For SwiftUI apps, you'll see that we referenced a NamiDataSource. You'll need to create this class and publish events to it to use throughout your application.

We'll discuss this more in the section Grant access to paid app features below.

Show a paywall

Now that you have the SDK configured, let's show a paywall in your app. This step requires that you have a live Campaign configured in the Control Center with a Paywall.

Campaigns are used to setup the various placements and contexts in your app where you may want to show a paywall.

To raise a paywall for a "default campaign", which is one without a label, just call launch like this:

NamiCampaignManager.launch()

If you campaign has a label, you can launch it like this:

NamiCampaignManager.launch(label: "your_campaign_label")

That's it!

For more advance integrations, launch provides a variety of callbacks to give you insight into the campaign, including paywall interactions.

If you're using Nami for purchase/subscription-management see below for details on how to gate access using our entitlement engine.

Grant access to paid app features

Once a user has made a purchase, you'll need to make sure to give them access to the content and features in your app that require a purchase. This is managed on the Nami platform through our entitlement engine.

The first option is to check whether a specific entitlement is active. This is done with the following code.

if NamiEntitlementManager.isEntitlementActive("premium_access") {
 	// allow access to premium app features 
}

Nami also triggers a callback any time there is a change to the state of an entitlement. In the callback the full list of currently active entitlements is provided. You can use this to store the state of whether a user has access to premium features locally in your app.

It is important that any callbacks are created as early in the app launch as possible. We recommend adding the callback handlers in the NamiSetup method in your ApplicationDelegate.swift or ApplicationDelegate.m.

NamiEntitlementManager.registerChangeHandler()  { activeEntitlements in
  for ent in activeEntitlements {
    let ent_id = ent.referenceID
    // use the active entitlements reference IDs to grant
    // access to premium app features
  }
}

That's all the basics to get up and running with Nami on Apple. For more use cases, explore the rest of our docs.

SwiftUI

In order to react to changes in the entitlement state of a user in your SwiftUI app, you need to create an ObservableObject you can use to store this state in your app.

The class below shows how to create the NamiDataSource object we used in the Configure the SDK section above. This code will work for any app that only has a single entitlement (the user has access to the paid content or not).

If your app has multiple entitlements or you need to store something like a credit balance, simply create additional @Published variables to store the data you need.

class NamiDataSource: ObservableObject { 
  @Published var purchased = false

  private var listener: NSObjectProtocol?
  
  init() {    
    purchased = (NamiEntitlementManager.available().count > 0)
 
    // react to changes to entitlement state
    NamiEntitlementManager.registerChangeHandler { (activeEntitlements) in
       self.purchased = (activeEntitlements.count > 0)
    }
  }
}

Now to react to the entitlement state of your users, simply reference the NamiDataSource object in your View and access any of the @Published variables. In this case, we simply need to check the boolean purchased.

struct ExampleView: View {
  @EnvironmentObject var namiDataSource: NamiDataSource
  
  var body: some View {
    if namiDataSource.purchased {
      // react to active entitlement 
    }
  }
}

Last updated