
*** NOTE: The in-app purchase items that are included in the sample code will only work if you are accessing the US app store. If you are outside of the US, you will have to create your own application and in-app purchase items, and overwrite the ID’s that we specify in the code. Thanks to reader Ben Rad for pointing out that it also will not work with jailbroken iOS devices! ***
Hey everyone – we’re in the midst of a project where we’ve just had to implement In-App purchases, so we figured we would get this quick little tidbit of info out to you all. What we’ve put together is a quick sample app based off of Ray Wenderlich’s tutorial. Ray’s tutorial was based off of UIViews, and we here at iPhone Game Tutorials love Cocos2d (download here!) so we ported this over. Hopefully this helps someone make some big bucks!
Source after the break!
You can download our example here. It should display a list of Ray’s in-app items and their price – you can click on the price to purchase the item. One important thing to note is you have to test this on a device – in app purchases do NOT work in the simulator! There’s still a couple other things you have to do before testing. From Ray’s site:
While you’re running your app in XCode, you’re not running against the real In-App Purchase servers – you’re running against the sandbox servers.
This means you can buy things without fear of getting charged, etc. But you need to set up a test account, and also make sure you’re logged out of the store on your device so you can see the whole process.
To make accounts, log onto iTunes Connect and click “Manage Users”. Click “Test User”, and follow the buttons to create a test user that you can make dummy in-app purchases with on the sandbox servers.
Then go to your iPhone and make sure you’re logged out of your current account. To do this, go to the Settings app and tap “Store”, and then click “Sign Out.”
Also, to test with your own app, you need to first create the app with a unique bundle identifier (like “com.yourcompanyname.appname”), then set the bundle identifier to match that in the Info.plist file. Then you’ll need to configure your new app for in-app purchases in the iOS Provisioning Portal under the App ID’s section, and finally create a new provisioning profile associated with the new app. Side note – you can do all this with an existing app if you like, as long as you configure it for in-app purchases and re-download/install the provisioning profile associated with that app.
Once you’ve got that all setup, you’ll want to change lines 28-32 in InAppRageIAPHelper.m to match whatever product identifier you gave your in-app products.
That’s really all there is to it – good luck and post any questions in the comments!
sending...
Hi,
Maybe im doing something wrong here, but ive downloaded your source code and ran it on my device. But all i get is a black screen nothing else.
Any help?
Hi Lee,
Interesting – Did you see any additional information in the logs?
hi did u fix the mistake
as i get the same issue
please help me
Im getting same error please help hollanc4@lsbu.ac.uk
sorry ignore last comment I was being a dumbass I fixed it
Hey,
I’m getting a strange error with my test account
” this account doesn’t have permission to make app purchases. You can change account permissions in iTunes connect. ”
The problem is I can’t seem to be able to change any settings with this test account :/
There are obviously a few things you’ll need to change to make it work for the project your working on, like changing the bundle and product id’s. We’ve only really did a basic integration into Cocos2d from the UIView app it was originally on Ray’s website. Not sure what might be the problem – Can you give any additional information?
Hi iphonegametutor.
All I’ve done so far is downloaded your test project and created one of those test accounts to try in app purchasing. I just want to see how it works before implementing it into one of my own projects.
B
Make sure you’re not trying to run this in the simulator, as testing in-app purchases won’t work there.
Im running it on my phone like you suggested, ive looked around on the internet but im sure ive gone through the same procedure.
Ive set up a test account in itunes connect but still get that error like above.
B
Hmm, you’ve got us stumped Brian. The only thing I can think of is that maybe the birth date of your test user is less than 13 yrs old and there’s some child purchase protection stuff that Apple has built in?
Ive checked all that aswell, everything seems to be ok. Ive deleted the accounts and made new ones the message i get now is:
‘your account is only valid for purchases in the uk itunes store’
I mean i havent changed anything apart from setting up an account in itunes connect.
I know this is an old thread, but I think I know what caused this, so I’ll leave this here in case it helps anyone else.
From the Connect doc…
“If you mistakenly use your new test user account credentials to log into a production environment on your test device instead of your test environment, your account credentials will become invalid and cannot be used as a test account again.”
I didn’t see this warning in the iTunes Connect documentation, and tried to log into the app store with my test account. I started getting the exact message you are. I’m not sure if there’s a solution, but I guess you could try phoning Apple.
Well that error message at least makes more sense – the in app purchase items must be set to only be available in the US store. You can create your own in app items in iTunes Connect, make sure your Info.plist has the proper bundle identifier, and change lines 28-32 in InAppRageIAPHelper.m to match whatever product identifier you gave your in-app products. Hopefully that helps you see some products!
I have also used this code, and ran it successfully.
Have you followed the instructions provided in last two paragraphs of this Post. Follow them to run the code for your own Product.
Hi,
Thanks for the code.
It runs perfectly as stancabile app (retrive products and prices) tien i tried to integrate it as follows:
1-add all my game classes into your code
2-renane your game scene in “in app scene”
3-change initial app scene into my menu scene
Unfortunately
Unfortunately when i launch “in app scene” (e.g. From “upgrade” button on main menu) i get a timeout error.
Any idea on how to’ solve this problem?
Thanks
Alex
Hi iPhoneGameTutor,
any ideas on how to fix my problem?
Thanks in advance for your help.
Alex
Hi Alex,
Right now it sounds like that the communication with the store isn’t happening (either apple isn’t responding with the product info or your connection timeout out near your end) – In the original tutorial posted by ray, he put in a 30 second timeout when that occurred. The best possible outcome would be that you check to see if the user has already bought the product in the UserDefaults and then show the ones he bought in any case (you don’t want to deprive your user of any products that they may have bought legitimately
I just get a black screen with fps display when i run the code
need help
Can you verify if you are accessing from the US? The default in app purchase items will only show for users in the US app store.
Im wondering if the black screen is only happening to people with IOS 5 installed, I get the same problem. Can anyone confirm??
Andrew, are you located in the US? The default in app purchase items will only show for users in the US app store.
No, Im using the UK app store, but it looks like you have already discovered that was the problem and have updated the tutorial to let other people know.
Thanks for the reply.
i am also getting a black screen, ios 4.3, xcode 4.1. why?
Are you in the US? The default in app purchase items will only show for users in the US app store.
Turns out you can’t use a jailbroken device, not even to load product IDs. Tried on my non-jailbroken iOS device and it worked.
Thanks for the follow up Ben – will add mention this in the tutorial!
Rather beneficial thanks, It is my opinion your trusty audience might want a lot more items such as this keep up the good work.
Great example! I’ve been looking for this!! However …
I get Timeout! over and over again.
I’ve tried on several different devices. I’m in the US store. I put my unique bundle identifier in there. I even created my own products instead of Ray’s but still nada.
I know Apple servers can be pretty bad but is there something else that could be causing this?
You can try increasing the timeout set in RootViewController on line 94:
to see if it is in fact a timeout or a problem communicating with the store. Try that and let us know what happens!
Thanks for such a fast response!
I tried that last night and still times out.
Just checked this morning several times hoping the Apple server is back up but keep getting timeout.
I’ve tried 4 different devices now and still no go :-/
I haven’t tried an actual phone yet so maybe it has to do with wifi connection?
Anyone else see this?
Got it working.
Be sure you give the full product ID in iTunes connect:
com.yourcompany.inapprage.myproduct1
I was simply putting myproduct1.
Hi.
Are you spanish?
I have the same problem. all time i get timeout and my screen is black
I’d revised all: itunes connect, test user, bundle identifier, app id, the items id… and not working.
Could you help me?
Anyone can help me? please
thanks.
Regards.
Hey, I have everything working properly but when I change the initial view in the app delegate from helloworld, to my main menu and use a button in the main menu to load hello world I just get a black screen and its not loading products, if i change the initial view back to hello world it works fine ?
Thanks for the tutorial and sourcecode.
However , I am getting the same issue as Callum.
Has anyone got this working in a view that isn’t the initial scene?
Hey Darren, I found a solution to the problem but that was about a month ago and for the life of me I cant remember, was somthing with placing the app delegate stuff in the menu screen, anyways I’ll look in the week at the code and get back to you here
Thank you!
Callum…. I guess you never got back on to post the solution?
Darren, Did you ever figure this out?
I’m seeing months of inactivity here so, hopefully I’ll figure this out on my own before months go by. If I figure it out I will be sure to post the solution on here. If anyone else stumbles upon this who has the answer, please share!
I’ve been successfully programming games using cocos2d for about six months now, however I still just have a really poor understanding of how to incorporate multiple API’s that all seem to require using the rootViewController in different ways. Eventually I finally figure out a way to work normally, but in the end, I still don’t understand it. Especially trying to use Kobold2d which is spectacular, until you try to integrate anything outside of the functionalities built into it.
I have the same problem. all time i get timeout and my screen is black
I’d revised all: itunes connect, test user, bundle identifier, app id, the items id… and not working.
Could you help me?
Anyone can help me? please
thanks.
Regards.
Jesus,
No I’m in the US, and still haven’t gotten this to work properly from cocos2d app. I’ve moved on as it’s simply taken me to much time at this point, and in the end I have no idea how profitable it would be in my game to add in an in app purchase. I think I’m just going to make this one a paid app and move on to next project. When and if I find the solution i’ll post it here. Please do the same.
Thanks!
PS. U said your spanish. I know more people jailbreak iphones outside tge US than within. Make sure you’ve noted that it was said it would not work on JB devices.
I’ve been able to get this to work”ish” a few times now, but it seems to be totally random when it does. i log in, log out of App Store. Make sure App is off when I do this. Sometimes I delete App and do rebuild. Sometimes I try standing on my head and praying to Budda. I can’t find a consistent series of actions to make this work.
Callum or Darren are you out there? Please help if you’ve found the answer to this. My fiancé is ready to leave me as I continue to struggle over and over and over to get this to work. I’ve found 10 other tutorials in countless google searches that never quite get me to the end zone. All I want to do is add an in App Purchase to Unlock all levels. I know my set up is right, because randomly this actually appears to work, but something is missing.
I would provide code, but I’m not sure what code to provide. I simply need a working example of an in App non-consumable purchase being requested from a scene using cocos2d. I don’t care if Ray’s uitable table pops up showing all available in app purchases, or if it handles this is some other way.
If anyone reading this has a working example I would really really really really appreciate seeing the source code on how to do this. I’m going on weeks now. I simply can’t figure it out……
Hey guys,
Sorry for taking a while to reply havnt had access to that mail box for a while, nix don’t let this programming stuff take over your life buddy you fiancé is more important! I have an app called ‘Theatre of conflict’ on the app store atm which was built using cocos2d and has a working in app purchase feature so this way will work. It’s not perfect, but it does the job. Send me a private message to hollanc4@lsbu.ac.uk and I will email you a working copy.
Peace, and Nix, go buy ur woman some flowers :p
Get to know the power of Unity 4′s character animation system, Mecanim, with this comprehensive example package. Inside, you’ll find eleven example scenes d… More Information
I found an easier way. There is a Cocos2d IAP Store creation tool that allows you to design a store based on layouts and presets and then export as an Objective C code so you can paste into your Cocos2d game.
Sweet! Thanks for this!
Hi, there is no RootViewController in cocos2d 2.x … where do I put the code in? it’s just helloworld, AppDelegate, IntroLayer… help plz