Powered by Squarespace
Development Blog Archive
CIRCUS BLAST

Test your aiming skills! 

Circus Blast -- 3D Puzzle Shooter. You're a monkey. In a circus. Trying to help the other animals escape, with a cannon!

 

 App. is no longer available. 

SUMO TAP

Enter the dohyou to challenge yourself to a fast pace game of Sumo Tap! Play online with friends, using Bluetooth and post your high scores to Twitter.

 

 App is no longer available.

Entries in in-app purchase (1)

Saturday
Mar062010

Adding In-App Store to Cocos2D

There's been a few good tutorials about setting up items for purchase on In-App Stores (http://blog.mugunthkumar.com/coding/iphone-tutorial-–-in-app-purchases/ and http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/ were helpful) but there were some missing pieces about how to create a singleton to manage the store and how to display the results. The design pattern we use follows some of the singleton conventions in Cocos2D, and we've used it to create an in-app store to allow users to purchase level upgrades and multiple character upgrades and display the store using either UIKit or Cocos2D.

As a first step, you need to enable your products for sale with In-App Purchasing, including creating a unique App ID and provisioning profile. Then you'll need to create your products.

To manage the display and purchase of the in-app purchases, we created the following singleton class using the Cocos2D design pattern:

InAppPurchaseManager.h

http://pastie.org/886863

 

InAppPurchaseManager.m

http://pastie.org/886892

The usage of this singleton is pretty simple. First, before calling your product display view/scene initialize the store with this call:

[[InAppPurchaseManager sharedInAppManager] loadStore];

This will asynchronously load the store data for later display. We use plists to store the in-app store products names and titles, and then use this information to display a simple summary via a UITableView. The tricky part is knowing when the product data has loaded. We could have used an NSNotificationCenter callback but instead decided to have a simple function to poll whether the store loaded. We poll this function with an NSTimer, repeating every 5 seconds:

if ([[InAppPurchaseManager sharedInAppManager] storeLoaded])
{
// Update the UI to show that the store has loaded, ie. the user can make a purchase
}

After the user selects an item they'd like to buy, we display a more detailed view/scene that loads descriptions from the in-app store and stores them in SKProduct records. We do this with calls to product specific methods within the singleton:

SKProuct *prod = [[InAppPurchaseManager sharedInAppManager] getLevelUpgradeProduct];

We've written methods for each of our products and then keep track of the product index (ie., the displayed list of products) using a plist so we can change the display order if we'd like to promote one product higher than another for example. Once the user is satisfied with the details of the product and decides to make a purchase, we call the

And during initialization of our scene/view, we register for NSNotificationCenter callbacks for the purchase success or failure:

 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(purchaseSuccess:) name:kInAppPurchaseManagerTransactionSucceededNotification object: nil];

 

Where the purchaseSuccess: method displays a simple thank you message. Last but not least, we need to provide a way for the user to restore previous purchases if they upgrade devices, etc. We needed to support this because our products are consumable. This simply means you need to eventually place a button in your in-app store scene/view which makes a call to this method:

 

[[InAppPurchaseManager sharedInAppManager] restoreCompletedTransactions];

 

With that, you're up & running. Good selling!

--yarri