First off a quick disclaimer - I’m still fairly new to iOS internals and so please take this code as a starting point and be aware that it MAY contain bugs. Feel free to comment, extend, advise as required but as Addon’s are required to get iAd’s, IAP, Analytics etc working I thought I’d document my experiments.
Note - the ultimate aim is to be able to implement IAP and Google Analytics (as discussed at http://www.raywenderlich.com/53459/google-analytics-ios) unless someone offers up a better solution.
Firstly here is an example program that we’re going to use to test our simple Addon.
-- Addon Testbed
---[[
-- These stub functions will be replaced by their "real" counterparts in the exported xCode project
-- Addon functions are grouped via a table
local cb = nil
myFirstAddon = {
extGetValue = function() return "INTERNAL" end,
extSendOneParamToDouble = function(a) return a end,
extSendTwoParamsToMultiply = function(a,b) return a+b end,
extSetCountBase = function(c) print("setCountBase "..c); cb = 9999 end,
extIncCount = function () cb = cb-1 end,
extGetCount = function () return cb end,
}
--]]
-- Use this function to perform your initial setup
function setup()
print("AddonTestbed")
print("getExtValue = "..myFirstAddon.extGetValue())
print("Internal 2 doubled = "..2*2)
print("sendOneParamToDouble(2) = "..myFirstAddon.extSendOneParamToDouble(2))
print("sendTwoParamsToMultiply(3,4) = "..myFirstAddon.extSendTwoParamsToMultiply(3,4))
print("setCountBase(0)")
myFirstAddon.extSetCountBase(0)
print("getCount = "..myFirstAddon.extGetCount())
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
textMode(CORNER)
fontSize(20)
text("extGetCount = "..myFirstAddon.extGetCount(),40,400)
myFirstAddon.extIncCount()
end
Paste this into a new Codea project called Addon Testbed and run it to make sure all is ok. Note the use of the —[[ at the top - this is so I can block out the code when it’s run via xCode.
What I’ve discovered is that the best (only?) way to access functions is to wrap them up inside a table and then the table get’s registered in your project later on.
Assuming you’ve got all this working then export Addon Testbed as an xCode project (long press on the project tile) and just accept the defaults.
(see you in part 2)