Documentation

iOS Library

Install Sweet Pricing with our client library. Follow our 4 step guide to use dynamic pricing and track purchase events in your existing iOS app.

Check out the tutorial on our blog that shows how to install Sweet Pricing in a Swift iOS mobile app.

1. Download the Client Library

This iOS client library is distributed as a CocoaPod.

Add DynamicPricing to the Podfile file in your project:

pod 'DynamicPricing', '~> 1.0'

You can also download the client side library, as well as the source code, from our Downloads page:

2. Initialization

Import the header for SWPDynamicPricing:

Objective-C Example:

#import <DynamicPricing/SWPDynamicPricing.h>

Swift Example:

import DynamicPricing

Before you can start recording data or obtaining pricing information, you must initialize the client.

First, create a SWPDynamicPricingConfiguration object, initilizing with your app key. Then, call the setupWithConfiguration method on SWPDynamicPricing:

Objective-C Example:

SWPDynamicPricingConfiguration *configuration = [SWPDynamicPricingConfiguration configurationWithAppKey:@"98b30d00aae1d61245698547b81d5692"];
configuration.trackApplicationLifecycleEvents = YES;
[SWPDynamicPricing setupWithConfiguration:configuration];

Swift Example:

let configuration = SWPDynamicPricingConfiguration.init(appKey: "98b30d00aae1d61245698547b81d5692");
configuration.trackApplicationLifecycleEvents = true;
SWPDynamicPricing.setupWithConfiguration(configuration)

Replace the string "98b30d00aae1d61245698547b81d5692" with your actual app key.

Where Can I Find My App Key?

The app key is a public identifier for your app. Sign into App Manager, select your app and view the 'App Overview' screen. You can see your app key listed on this page.

App Overview Page shows 32 character hexadecimal app key.

3. Fetch the Product ID

Before you retrieve product information with Apple's APIs, you will need to query the current Product ID from Sweet Pricing. The product ID is no longer a fixed constant, but can change whenever in-app pricing is updated from App Manager. If your app is using segmentation, the product ID will correspond to the segment the user is in.

Use the fetchVariant method, on the shared DynamicPricing instance, to fetch product IDs for a particular in-app store. Pass a completion block which will be called once the network request completes.

This will provide you an instance of SWPVariant (even if there is an error).

Call the skuForProductId method to find the product identifier that you need to send to Apple's API.

Objective-C Example:

// We get an entire product group worth of App Store IAP IDs.
// These are the integer identifiers created by Sweet Pricing.
int productGroupId = 16;
int productId = 87;

// When we need to obtain the App Store product ID, send a request to
// fetchVariant to get pricing data for a particular product group.
[[SWPDynamicPricing sharedDynamicPricing] fetchVariant:productGroupId completion:^(SWPVariant *variant, NSError *error) {
  // The error argument is nil if there is no error.
  NSLog(@"Variant ID is %@", [variant id]);

  // Even if there is an error, a \SWPVariant object is returned.
  // If there was a problem getting pricing info, the method will fallback
  // on a default product ID.
  NSString *appStoreId = [variant skuForProductId:productId withDefault:@"com.sweetpricing.default.sku"];
  NSLog(@"Product SKU is %@", appStoreId);
}];

Swift Example:


SWPDynamicPricing.sharedDynamicPricing().fetchVariant(16) { (variant, err) in
  if (err != nil) {
    return
  }

  let productId = variant.skuForProductId(87, withDefault: "com.sweetpricing.default.sku")
}

If there was a problem retrieving the product ID information, you can choose to continue gracefully and use a hard-coded default product ID. (Or, if you prefer, present an error message to the user or log the error.)

Notice that productId is the integer identifier that Sweet Pricing creates.

Please note that you only have to fetch the variant information per in-app store. For example, if you have a group called 'Coins', you can fetch all coin price information in a single request.

4. Track Events

For Sweet Pricing to work, you need to record a number of events within your application. These events are periodically uploaded to Sweet Pricing, and are reflected on the 'Analytics' tab.

4a. Track View of In-App Store

Whenever you display the prices from an in-app store, you should record that event using trackViewStore. You can pass an instance of Variant to this function.

Objective-C Example:

NSArray *products = @[@{
  @"price" : [NSDecimalNumber decimalNumberWithString:@"9.99"],
  @"currencyCode" : [[NSLocale localeWithLocaleIdentifier:@"en_US"] objectForKey:NSLocaleCurrencyCode],
  @"productId": apple1MonthId
}, @{
  @"price" : [NSDecimalNumber decimalNumberWithString:@"29.99"],
  @"currencyCode" : [[NSLocale localeWithLocaleIdentifier:@"en_US"] objectForKey:NSLocaleCurrencyCode],
  @"productId": apple1YearId
}];

[[SWPDynamicPricing sharedDynamicPricing] trackViewStore:variant products:products];

Swift Example:

let productInfos = products!.map({ (product) -> Dictionary<String, AnyObject> in
  return [
    "price": product.price,
    "currencyCode": product.priceLocale.objectForKey(NSLocaleCurrencyCode)!,
    "productId": product.productIdentifier
  ]
})

SWPDynamicPricing.sharedDynamicPricing().trackViewStore(
    variant, products: productInfos)

When you call this depends on the flow of your application. If you have one screen which shows all prices, then you can track this event when the user views the screen. For more complex schemes, you might wish to load the variant when the application starts and track a view variant event at this time.

If you are at all uncertain about when you should record this event, please contact support who will be able to look at your app and advise your further.

4b. Track Purchases

If the user makes an in-app purchase, you will need to call trackPurchase to record this event. You must pass the product ID that was purchased.

Objective-C Example:

[[SWPDynamicPricing sharedDynamicPricing] trackPurchase:appStoreId];

Swift Example:

SWPDynamicPricing.sharedDynamicPricing().trackPurchase(transaction.payment.productIdentifier);

Only track this event when the purchase is complete (rather than, for example, the user clicking 'Buy').

It is generally assumed that the variant the user saw prior to purchasing the product was the one most recently recorded using trackViewVariant

4c. Track Arbitrary Events

You can use the track function to record other events that might be of interest to your analytics or dynamic pricing.

[[SWPDynamicPricing sharedDynamicPricing] track:@"Logged In"];