Using IAP to remove adverts

Hi there,

I am fully comfortable with implementing IAP upto the point where the purchase is made.

Once a purchase is made, how do I make it do what I want?

I’m kinda trying to use the code below, but have no idea to set the isPro variable to true if a purchase is made, or how to remove ad’s

Can anyone point me in the right direction on how to implement this?

[lua]–runs on startup
local isPro = false

local function displayAd()
if isPro == false then
ad.init(blah,blah)
main()
elseif isPro == false then
main()
end[/lua] [import]uid: 62706 topic_id: 30111 reply_id: 330111[/import]

@CraftyDeano, there are a couple of ways to do this as far as I know. In the “purchased” state of your IAP code, call a function that will do what you want it to do to handle it. And the function would either:

  1. save the purchased state (much as you may save a game state) that you’d retrieve each time you launch the app (or each time you want to initialize the app) – you may want to look for save functions in code share if you are not sure how to do it, or…

  2. call a network request and save the status of the user in the backend server (which you will check every time you launch the app) – http://docs.coronalabs.com/api/library/network/request.html
    Edit : The network request version would need to incorporate “save” method to ensure there’s no hiccup when there’s no wifi/internet connectivity.

Either way, you need to be able to access the variable that saves the isPro false/true state. I hope this makes sense to you.

Good luck.

Naomi
[import]uid: 67217 topic_id: 30111 reply_id: 120541[/import]

Thank you, very helpful as always :slight_smile: [import]uid: 62706 topic_id: 30111 reply_id: 120547[/import]

@CraftyDeano, glad to hear I could help.

BTW, you would want to think through how you may stop showing ads after the purchase and before the app is relaunched/re-initialized. I imagine the purchase would take place after the app is already initialized to show the ads.

Cheers,
Naomi [import]uid: 67217 topic_id: 30111 reply_id: 120553[/import]

Yep, I got that part covered, running ad.hide() after purchase, then checking a local file/database when the app reboots. That’s my weekend planned :slight_smile: [import]uid: 62706 topic_id: 30111 reply_id: 120556[/import]

From personal experience, I’ve found that IAP to remove ads simply doesn’t work. You need to provide compelling IAPs such as power-ups, in-game currency, etc. [import]uid: 36054 topic_id: 30111 reply_id: 120572[/import]

If I can figure it out and get it working, ill let you know and share the code.

I cant really see any reason why it wouldn’t work, but I have a few ideas in my head that might work. [import]uid: 62706 topic_id: 30111 reply_id: 120628[/import]

Ok - I have cracked it and it works perfect.

isPro is a variable checked at startup to see if pro has been purchased, its false by default
I have my adverts run in an if statement (if isPro == false show adverts, else end)

The below code checks to see if a file (pro.txt) exists, if it does isPro is set to true.

[lua]local isPro = false

function checkisPro()
local path = system.pathForFile( “pro.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )
if file then
isPro = true
io.close( file )
end
end[/lua]

When my purchase button is pressed and comes back as purchased, the following code runs:
[lua]writeproFile() --runs function below
ads.show( “banner”, { x=5000, y=5000, interval=30 } ) – moves adverts while app is still running as ads.hide is broken

function writeproFile() – creates a file called pro.txt to check on fresh startup
local path = system.pathForFile( “pro.txt”, system.DocumentsDirectory )
file = io.open( path, “w” )
file:write( “Hey, I just bought you, and this is crazy, Heres my credit card number, Charge me maybe?” )
io.close( file )
end[/lua] [import]uid: 62706 topic_id: 30111 reply_id: 120753[/import]

I’m also interested in possible solutions.

@CraftyDeano your solution works, but I was wondering, what’s stopping the user from using some 3rd party tool or SSH to access the file system and manually adding the file “pro.txt”?

I guess what I am saying is, is there a more secure way to save game states that are unlocked with IAP? I know it is most unlikely that there is a crack proof method, but any way to at least make it difficult? [import]uid: 108204 topic_id: 30111 reply_id: 120843[/import]

@timespacemagic Another option would be to do the same thing but query an SQL database on the device.

Maybe the pro.txt could contain a random hash generated against something unique to the device (or receipt number)?

Also I have the filename set to something different then pro.txt, I would suggest removing the .txt extension. [import]uid: 62706 topic_id: 30111 reply_id: 120845[/import]

@CraftyDeano I have thought about that too, I previously used and sql database to store game states, and it worked great. It’s a little less straightforward than adding a pro.txt, but I believe it’s still possible to simply open the sql3 database to edit the values. If only Corona had a way to encrypt the sql3 database like SQL Cipher etc.

I also considered your idea of using a unique ID. It would make perfect sense to call system.getInfo(“deviceID”) and hash the UDID, but I briefly read somewhere on the forums that Apple is taking a stance against using UDID, and rejects apps that make the API call.

I feel like I’m being paranoid about all of this, but you never know man :stuck_out_tongue: [import]uid: 108204 topic_id: 30111 reply_id: 120851[/import]

Hmm, could use ( system.getInfo( “name” ) ). It would return ‘Deans iPhone’ and is fairly unique, as UDID is being depreciated but apparently going to be replaced with something else?

Only problem is that users can change the phone name on the fly (although I have always used default and not needed to change it) causing the validation to fail, however restoring the purchase could resolve that.

Edit:

After a bit of research I think this is what ill implement something like the below:

[lua]function checkifPro()
local deviceName = ( system.getInfo( “name” ) )
local cryptoKey = “SuperSecretPassword”
local fileName = crypto.hmac( crypto.md5, deviceName, cryptoKey)
local path = system.pathForFile( “fileName”, system.DocumentsDirectory )
local file = io.open( path, “r” )

if file then
isPro = true
io.close( file )
end[/lua] [import]uid: 62706 topic_id: 30111 reply_id: 120856[/import]

@CraftyDeano, there are a couple of ways to do this as far as I know. In the “purchased” state of your IAP code, call a function that will do what you want it to do to handle it. And the function would either:

  1. save the purchased state (much as you may save a game state) that you’d retrieve each time you launch the app (or each time you want to initialize the app) – you may want to look for save functions in code share if you are not sure how to do it, or…

  2. call a network request and save the status of the user in the backend server (which you will check every time you launch the app) – http://docs.coronalabs.com/api/library/network/request.html
    Edit : The network request version would need to incorporate “save” method to ensure there’s no hiccup when there’s no wifi/internet connectivity.

Either way, you need to be able to access the variable that saves the isPro false/true state. I hope this makes sense to you.

Good luck.

Naomi
[import]uid: 67217 topic_id: 30111 reply_id: 120541[/import]

Thank you, very helpful as always :slight_smile: [import]uid: 62706 topic_id: 30111 reply_id: 120547[/import]

@CraftyDeano, glad to hear I could help.

BTW, you would want to think through how you may stop showing ads after the purchase and before the app is relaunched/re-initialized. I imagine the purchase would take place after the app is already initialized to show the ads.

Cheers,
Naomi [import]uid: 67217 topic_id: 30111 reply_id: 120553[/import]

Yep, I got that part covered, running ad.hide() after purchase, then checking a local file/database when the app reboots. That’s my weekend planned :slight_smile: [import]uid: 62706 topic_id: 30111 reply_id: 120556[/import]

From personal experience, I’ve found that IAP to remove ads simply doesn’t work. You need to provide compelling IAPs such as power-ups, in-game currency, etc. [import]uid: 36054 topic_id: 30111 reply_id: 120572[/import]

If I can figure it out and get it working, ill let you know and share the code.

I cant really see any reason why it wouldn’t work, but I have a few ideas in my head that might work. [import]uid: 62706 topic_id: 30111 reply_id: 120628[/import]

Ok - I have cracked it and it works perfect.

isPro is a variable checked at startup to see if pro has been purchased, its false by default
I have my adverts run in an if statement (if isPro == false show adverts, else end)

The below code checks to see if a file (pro.txt) exists, if it does isPro is set to true.

[lua]local isPro = false

function checkisPro()
local path = system.pathForFile( “pro.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )
if file then
isPro = true
io.close( file )
end
end[/lua]

When my purchase button is pressed and comes back as purchased, the following code runs:
[lua]writeproFile() --runs function below
ads.show( “banner”, { x=5000, y=5000, interval=30 } ) – moves adverts while app is still running as ads.hide is broken

function writeproFile() – creates a file called pro.txt to check on fresh startup
local path = system.pathForFile( “pro.txt”, system.DocumentsDirectory )
file = io.open( path, “w” )
file:write( “Hey, I just bought you, and this is crazy, Heres my credit card number, Charge me maybe?” )
io.close( file )
end[/lua] [import]uid: 62706 topic_id: 30111 reply_id: 120753[/import]

I’m also interested in possible solutions.

@CraftyDeano your solution works, but I was wondering, what’s stopping the user from using some 3rd party tool or SSH to access the file system and manually adding the file “pro.txt”?

I guess what I am saying is, is there a more secure way to save game states that are unlocked with IAP? I know it is most unlikely that there is a crack proof method, but any way to at least make it difficult? [import]uid: 108204 topic_id: 30111 reply_id: 120843[/import]