When to load products on iOS?

Due to my recent work for iOS I’ve noted that the plugin for IAP on this platform doesn’t have an “init”(event.name) event like android, so in order to load the products should I have to implement something like this?:

-- Initialize Apple IAP
store.init( transactionListener )

-- Perform steps to enable IAP, load products, etc.
timer.performWithDelay(200, function()
store.loadProducts( ... )
end)

Another doubt:
If at the time of executing the store.init( transactionListener ) the user does not have internet, will it be necessary to execute the same function later?

  1. Yes, this looks correct.
  2. If user does not have internet nothing will happen, so yes you need to execute the same function. In my experience i never had issues with that.

Before implementing IAP, it is important to read all related documentation and understand every function. And also you need to play with it on a device. IAP is not an easy task. Be ready to spent some hours or even days to make this work.

Docs: Solar2D Documentation — Plugins | google-iap-billing-v2
Good example: GitHub - solar2d/com.solar2d-plugin.google.iap.billing.v2
In: src/Corona/main.lua you can test states (works on real device)

Also use the last updated plugin:

{
    plugins = {
        ["plugin.google.iap.billing.v2"] = {
            publisherId = "com.solar2d",
        },
    },
}

We’ve been handling it by polling to check if the store was inited and then using a blocking variable to stop the polling after we are able to load products. Something like this:

function IAP.update()
	if(targetAppStore=="apple")then
		if(store.isActive and store.canLoadProducts and iosStoreInited==false)then
			--populate a local table of just product IDs and call the loadProducts function
			local ids={}
			for i=1,#productIdentifiers do
				ids[#ids+1]=productIdentifiers[i].id
			end
			store.loadProducts(ids,productListener)
			iosStoreInited=true
		end
	end
end

The IAP.update function is called every frame

1 Like

Thank you @famousdoggstudios , I like your implementation, but why don’t you execute the loop that fill the ‘ids’ table out of the function? … it will optimize your code.
BTW, according to the documentation of the Apple IAP in this page https://docs.coronalabs.com/plugin/apple-iap/init.html the store.init() in apple also call the listener with the “init” event, but that event is not included in the documentation.

I think the documentation is a little ambiguous due to mention of Google IAP in the same page but what I understand is that it will not send an init state event for iOS transaction listener events.

I had written the code a while back but I think that was the main reason I had to keep polling to check if the init was successful for iOS.

As for the loop that populates the IDs, you’d notice that I’m using a local boolean “iosStoreInited” as a lock so once that flag is raised, that loop will not be executed again.

1 Like

Agreed with you, the documentation is a little ambiguous. I’ve opened an issue on Github for that.
Sorry, I noted now the boolean variable

Hey @famousdoggstudios I’ve just tested it and I can confirm that the event.name = “init” exists for iOS too.

Thanks for posting this. I might try this on my next build if required although I would probably not change my code unnecessarily since it’s working so far and these implementations can often be very sensitive to changes so I’d probably just let it be :slight_smile:

1 Like