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.

« Ruby on Rails For iPhone | Main | iPhone Facebook Integration on Rails »
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

Reader Comments (13)

Glashtte Primary represents a living, genuine manufactory, high exclusivity, traditional watchmaking since 1845, exclusive mechanical masterpieces, along with handmade in Germany.
replica breitling

September 19, 2010 | Unregistered Commentervko

Great thanks!

This is a bookmark :)

October 2, 2010 | Unregistered CommenterMike

Great post, and source code. Thank you!

I added the singleton to my project, and it all seems to work well, but it attempts to purchase the product twice, as if finishTransaction isn't being called. You say in the post that you add an observer called purchaseSuccess. Is this method supposed to call your completeTransaction function so that finishTransaction is called? If so, how do you get the transaction details?

Cheers,
Andrew.

November 11, 2010 | Unregistered CommenterAndrew

iwc spitfire
can make you feel good, and that may improve your grade. A successfui businessman must have one kind of watch to show his special quality. for a lady the watch also can attract many eyes. that is a amazing feeling for owning a watch. these watches are selected for all kinds of people. you can get your favorite watch freely. replica a.lange & sohne

July 12, 2011 | Unregistered Commenterlisayun

Thank you. I just found this blog and what a great resource! It is a good blog and their posts are effective. This is very nice post! I will bookmark this blog. lpuvpn lpuvpn - Justin Bieber Shoes.

October 14, 2011 | Unregistered Commentergeyzgs geyzgs

Who the fans of Hustle you should know that the story of a group of elite con artists, who each week pull off a complicated scam with several twists. Mickey is the group's leader, who comes up with the plans. Albert is a veteran con man who in his later years is a "roper," searching out suitable victims and introducing them to his cohorts. Ash comes up with all the locations and equipment needed for the cons. Stacie steps in whenever she needs to use her feminine wiles. The group is rounded off by Danny, a young amateur grifter who Mickey takes under his wing. Hustle Season 6 DVD Boxset includes episodes that when the hustlers get caught they are offered an amnesty by MI6 officers Jenny and Nigel if they will steal a safe box from the vault in the National Bank of Syria. However the couple are hoping the team will steal money from a second vault so they can be re-arrested. Sean poses as a bank client to discover a way in and the hustlers make use of Ash's gadgetry to penetrate the vault while Albert creates a diversion.The mission is accomplished in terms of acquiring the box but Jenny and Nigel still have to be taught to back off.How wonderful it is,if you want to get it,just visit the site collectcheaps ,where you can get the good quality dvds spending low price.

October 25, 2011 | Unregistered CommenterHustle Season 6 DVD Boxset

Disney is the one who make our dream come true as he made a wonderland for all people and a series of cartoons leading to variety characters. The Vampire Diaries dvd

October 31, 2011 | Unregistered CommenterThe Vampire Diaries dvd

Is there any superstar around you, or he really exist around you, but you never recognized he is the superstar you love so much. Full House dvd

November 1, 2011 | Unregistered CommenterFull House dvd

I'd actually love to review these boxes on my shopping/lifestyle blog. i wonder if they'd send me a sample box to talk about? nzolta nzolta - Moncler Daunenjacke.

November 21, 2011 | Unregistered Commenterpzxxyz pzxxyz

Great information, the links in your post has where very useful. I think people claim for every little thing these days and it is really no need to.I thing Submit Your Ideas for the Myrtle Public Pedestrian Plaza is nice preparation.I really like this post.I will bookmark this site.

This blog post was absolutely fantastic. Thanks for taking the time to debate this, I feel strongly about it and love learning extra on this topic.

December 27, 2011 | Unregistered CommenterDenver Signature Limousine

I Really enjoyed your blog. I just bookmarked it. I am a regular visitor of your website I will share It with my friends .Thanks.......

Thanks for this post. It's proving to be very useful. Putting In-App purchase in cocos2d was a PITA until I came across your post. I'm not done implementing it yet but i got further with your example than with any other I could find.

I am creating grenades that the user can buy in packs of five in the game. How can you keep track of how many grenades the user has bought so far, then subtract one each time he uses a grenade?

February 28, 2012 | Unregistered CommenterMarc Renaud

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>