Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/badjaji/public_html/blog/wp-includes/plugin.php on line 600

Corona SDK store.init was not working for Apple App Store IAP (in-app purchases)… or so I thought!

I’m implementing IAP for the first time for the Teen and Adult Phonics Library app I’ve developing in collaboration with Victoria Leslie.  The app was built using the Corona game engine (also used for my literary game WordWhile)  I just had a realization about the API that came to me after about two hours of pulling my hair out.  Hope reading this save someone some time and  frustration and self-doubt and…

There are many important details that need to be set-up correctly to use the Corona store.* API for IAP (in-app purchases) on the App Store, so when my implementation was not working, I thought I’d gotten one of these details wrong. I spent a few hours going over everything with a fine-toothed comb and rereading the guides (staring with Corona’s IAP guide).  But there was something fundamental that I misunderstood about  store.init and I want to share my realization here.

When you call

     store.init( transactionListener )

the listener function is not called once the store is initialized.  I thought the listener was called when initialization completed, but that’s incorrect. You call the store.init() method and then you can call the other store.* methods right away, or so it seems.  To be on the safe side, you could make the call to store.init() and wrap the first call to another store.* method in a function that’s called in timer.performWithDelay().  In my tests, however, the code below works without introducing any delay.

local store = require 'store'

local function transactionListener( event )
    -- handle outcome of transaction (like store.purchase() )
    store.finishTransaction( event.transaction )
end
store.init( transactionListener )

if store.isActive and store.canLoadProducts then
    local function loadListener( event )
        if event.products then
            for i=1,#event.products do
                print('Valid ID: ', event.products[i].productIdentifier )
            end
        end
    end
    local productIdentifiers = { 'com.newb.insights' }
    store.loadProducts( productIdentifiers, loadListener )
end

I’ll try to suggest that this be clarified in the API documentation, but in the meantime, I hope this post can help others who, like me, assumed that you had to wait for store.init() to do its thing and let you know when it when store.* was ready.

Leave a Reply

Your email address will not be published. Required fields are marked *