So many people have been asking for an IAP add-on. I don’t have an add-on specifically, but more like instructions on how to get it set up in Xcode after you export your game into there to get ready to release it on the App Store. I compiled some old code from @Zoyt and his game Stack-it and another developer, CodeMonsters, who put his code up on GitHub for community use with his game Paper Wars. I had to figure out how to get them both working together for the newer version of Xcode, iOS 8+ and Codea, which is compiled here. This is months of progress wrapped into a nice and neat comment, so hopefully it all makes sense. I’m happy to share it with everyone as I wish I would have had it when I was looking for it!
IAPs in Action from Codea & Xcode!
To see it actually working from a real Codea project, check out my game, Color Troll, on the App Store, at http://www.colortroll.com/download.
IAP Instructions for XCode and Codea
Export your code to Xcode.
Make sure it runs fine in the Simulator or on your device.
With an App Developers license, in iTunes Connect, create your product IDs. You will need to “Archive” it in Xcode and upload it to iTunes Connect (pretty sure you have to get your app uploaded to iTunes Connect in order to add IAPs). It’s good just to have this completed once so you are familiar with archiving the app and seeing it on the iTunes Connect page. Be sure you stick with the guidelines and read over the handbook for which type of product it will be. Read all of this!
Be sure to make note of your app’s BundleID, SKU and Apple ID number as you will need these for the products you create.
Create your IAP products by following Apple’s directions here. Take note of the 10 digit number that corresponds to the product you want to sell. I have three in my game, so I will work with how to do more than one IAP product.
In your project in Xcode, under “Capabilities”, add the “Store Kit” by ticking “In-App-Purchases.”
Should look like this.
Under the “ProjectName/ProjectName/Addon” group on the left-hand side of the default view, create the new group, “ExtraiAP-master”. Insert these files in there. Link
- DefaultInitializer.h
- DefaultInitializer.m
- iAPManager.h
- iAPManager.m
- iAPProduct.h
- iAPProduct.m
In the group, “Extra”, add these two files:[Link above]
- IAPAddOn.h
- IAPAddon.m [includes rating function & language differentiation if you want to use it]
Should look like this.
In the “Addons” folder, in the “AppDelegate.mm” file, at the top, add this line of code under the other #imports.
#import "IAPAddOn.h" //ADD THIS FOR IAPs
Then add this line in the @implementation just below that with all the other self.viewController’s.
[self.viewController registerAddon:[IAPAddOn sharedInstance]];
Add to Main.lua class at the very end to create functions from Obj-C to work in Lua.
function getProductInfo(id,price)
iapItems[id] = {
price = price
}
end
function productBeingPurchased(id)
purchasePending = true
end
function restoredProducts(id)
setProductPurchased(id)
end
function productPurchaseFailed()
purchasePending = true
end
function productPurchaseSucceeded(id)
purchasePending = true
setProductPurchased(id)
end
function setProductPurchased(id)
if id == "AppID Name, like com.YourCompany.IAPname" then
--code for what happens in game, like adding 50 gold to the total, or whatever purpose the IAP is supposed to do.
elseif id == "AppID Name, like com.YourCompany.IAPname2" then
--what the next IAP does in app.
end
end
Add these to the front of the Main.lua setup() function
iapItems={} --added this for IAP
registerItem("AppID Name, like com.YourCompany.IAPname")
registerItem("AppID Name, like com.YourCompany.IAPname2")
initStore()
purchasePending = false
Not necessary, but you might want to include an internetConnected Boolean to make sure they are connected to the internet. Something like these functions. I used them for the IAPs and for the current time in UTC for no “time-hacking” on their device.
function getTime()
tbl={["headers"]="heading"}
http.request("https://www.google.com",getHttpData,httpError,tbl)
end
function httpError(error)
--no internet connectivity
internetConnected=false
end
function getHttpData(data,status,headers)
--internet is connected now
internetConnected=true
end
I will try and make a tutorial video soon to make it easier to follow along, if there is enough demand for it. If there is anything else that needs to be added or changed, as I did this from my detailed notes I took through the whole process and then copied it over, let me know. As long as I wrote them down correctly, it should work just fine. Good luck! And happy coding!