Setup AppHud for in-app purchases

Setup AppHud for in-app purchases

To set up AppHud for iOS using Swift, you need to follow several key steps. This guide will walk you through the process from adding the SDK to your project to initializing it in your app. Please note that the instructions provided here are based on the documentation available as of my last update and may need adjustments based on the latest AppHud SDK version and Swift updates.

 

Step 1: Add AppHud SDK to Your Project

 

Using CocoaPods

  1. If you haven't already, install CocoaPods on your machine. Open Terminal and run:

    sudo gem install cocoapods
    
  2. Navigate to your project directory in Terminal and create a Podfile if you don't have one:

    pod init
    
  3. Open your Podfile in a text editor and add the AppHud SDK:

    platform :ios, '11.0'
    target 'YourTargetName' do
      use_frameworks!
      pod 'ApphudSDK'
    end
    
  4. Save the Podfile and run the following command in Terminal to install the AppHud SDK:

    pod install
    
  5. Open the .xcworkspace file in Xcode to work on your project.

Using Swift Package Manager (SPM)

  1. In Xcode, go to File > Swift Packages > Add Package Dependency.

  2. Enter the AppHud SDK repository URL:

    https://github.com/apphud/ApphudSDK
    
  3. Choose the version range or specific version you wish to use and finish the setup.

 

Step 2: Initialize AppHud in Your Application

 

  1. Open your AppDelegate file.

  2. Import ApphudSDK at the top of your file:

    import ApphudSDK
    
  3. Initialize AppHud in the application(_:didFinishLaunchingWithOptions:) method:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Apphud.start(apiKey: "YOUR_APPHUD_API_KEY")
        return true
    }
    

Replace "YOUR_APPHUD_API_KEY" with your actual AppHud API key.

 

Step 3: Set Up In-App Purchases in App Store Connect

 

  1. Go to App Store Connect and log in with your Apple Developer account credentials.

  2. Select "My Apps" and choose the app for which you want to set up the subscription.

  3. Select "Subscriptions" in Monetization section from the sidebar menu.

  4. Click the "+" button to create a new subscription group.

  5. Reference Name: Enter a name that will help you identify this subscription in App Store Connect. This name is not visible to your subscribers.

  6. Product ID: Enter a unique ID for your subscription. It's a good practice to use a reverse-domain style identifier (e.g., com.yourcompany.app.weeklysubscription).

  7. Click "Create".

  8. Subscription Group: If this is your first subscription, you'll need to create a new subscription group. Subscription groups allow users to move between subscription options within the same group.

  9. Subscription Prices: Click "Add" to set the price for your subscription. You'll be able to select the subscription's price tier. Apple provides a Pricing and Availability section where you can see how much the subscription will cost in different countries.

  10. Localization: Add localized titles and descriptions for all the languages your app supports. This information will be visible to your subscribers.

 

Step 4: Set Up a Trial Period

 

  1. In the "Subscription Duration" section, select "1 week" as the duration of your subscription.

  2. In the "Subscription Prices" section, click "Create Introductory Offer" to set up a trial period.

  3. Select Countries or Regions for Introductory Offer.

  4. Choose Start Date and for End Date select “No End Date”.

  5. Select Type of Introductory Offer - Free, specify the duration of the free trial (e.g., 3 days).

  6. Once you've configured all the details of your subscription, including the trial period, review all the information to ensure accuracy.

  7. Click "Save".

  8. Your new weekly subscription with a trial period is now configured. However, it will need to go through Apple's review process before it becomes available to users.

 

Step 5: Create Products in Apphud

 

After defining your IAPs in App Store Connect, you'll need to create corresponding products in Apphud.

  1. Log in to your Apphud account.

  2. Navigate to the Products section.

  3. Click on the "+ Product" button to add a new product.

  4. Enter the Product ID from App Store Connect for each product you want to manage through Apphud.

 

Step 6: Configure a Paywall in Apphud

 

Apphud allows you to create and manage paywalls directly from their dashboard, enabling you to control when and how your subscription offers are presented to users.

  1. Go to the Paywalls section in your Apphud dashboard.

  2. Click on the "+ Paywall" button to start creating a new paywall.

  3. Specify the Paywall identifier you will need to specify it further in the code

  4. Design your paywall by adding the products you created earlier. You can customize the appearance, decide which products to include, and set up the order in which they appear.

  5. Save and activate your paywall. Once configured, your paywall is ready to be integrated into your app.

 

Step 7: Integrate the Paywall in Your iOS App

 

To display the paywall you've configured in Apphud within your iOS app, you need to use the Apphud SDK. Ensure you have already integrated the SDK as per the initial setup instructions.

  • Fetch and display the paywall at the appropriate moment in your app with Paywall identifier you created earlier. This could be in response to a user action, such as tapping on a "Subscribe" button, or when a user attempts to access premium content.
func products(completion: @escaping (([ApphudProduct]) -> Void)) {
        Apphud.paywallsDidLoadCallback { (paywalls) in
            // get paywall by identifier
            if let paywall = paywalls.first(where: { 
$0.identifier == "**YOUR_PAYWALL_IDENTIFIER**" }) {
                completion(paywall.products)
            } else {
                completion([])
            }
        }
    }
  • Handle purchase actions within your paywall UI by calling Apphud.purchase() when a user decides to buy a subscription or a product.
func purchase(product: ApphudProduct) {
    Apphud.purchase(product) { result in
        if let subscription = result.subscription, subscription.isActive {
            // Handle successful subscription activation
        } else {
            // Handle purchase error or invalid subscription state
        }
    }
}


Step 8: Test Your Implementation

  • Use sandbox accounts to test the purchase flow and ensure the paywall displays correctly under different conditions.
  • Verify that subscriptions activate as expected and that users can restore purchases if they reinstall the app or switch devices.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.