app fails to open/read file

Hi,

I have an app with iap integrated. The iap allows the user to upgrade the app to the full version (so it is a one time purchase).

When the user ‘buys’ the iap upgrade, the app creates and saves a file on the phone indicating that the upgrade had been purchased. That way, upon app start up, the app checks the file to see whether it should ‘unlock’ the full content. So from a buyer’s point of view, every time they run the app, it should be the full version.

However, sometimes, when the buyer starts the app, they get the ‘lite’ version instead of the full content despite having run the app with ‘full’ content multiple times before. This happened with more than one user. It also happened multiple times with two of them. Usually, when this happens i ask the users to ‘tap’ the dedicated ‘restore purchase’ button to resolve the issue, which it does.

My analysis is that it seems that sometimes the app can’t locate, open or read the file and hence will revert to the ‘lite’ version. But my question is why does it work most of the times and fails to do so sometimes for the same user on the same device?

Here is my code for saving the file after successful purchase:

saveStateToFile = function()     -- write isFullVersion to file     -- will write 'true' or 'false'     local path = system.pathForFile("purchase", system.DocumentsDirectory)     local file = io.open( path, "w")                 file:write( tostring(\_g.isFullVersion) )             io.close( file )     file = nil end  

Here is my code for reading the file upon app start up:

loadPurchaseSettings = function()     -- load purchased settings     local file         local str     local path     path = system.pathForFile( "purchase", system.DocumentsDirectory)     file = io.open( path, "r")     if file then                 str = file:read( "\*l" )                 if str ~= nil then                      if str == "true" then                 \_g.isFullVersion = true             else                 \_g.isFullVersion = false                         end                     else             \_g.isFullVersion = false         end         io.close( file )     end end  

(Note: if the upgrade has never been bought before, then no file should exist. The file will only exist after a ‘purchase’ has been initiated regardless of the purchase outcome.)

Any ideas on how to resolve this? Any insights?

Thanks

Luay

You never handle the “I couldn’t open the file” case.  If file is nil, you just return and you don’t set the isFullVersion flag.  Also when you write the file, you never check to see if you successfully opened the file for writing either.

I would suggest some print statements to see what’s going on too.

Rob

@Rob, Thanks for your response

The ‘isFullVersion’ flag is set to false before entering the ‘loadPurchaseSettings’ function thus, in my opinion, eliminating the need to address most cases that will result in setting the flag to false.

In any case, the above point doesn’t address my scenario. I have to think about this further.

I would guess the file is getting corrupted for some reason (really old phones maybe?)… if so, the file would probably still exist, but the content would be messed up.

Maybe you should stop checking if the content is “true”, and just test if the file exists?

@marcior: exactly what i was thinking. 

Thanks

You never handle the “I couldn’t open the file” case.  If file is nil, you just return and you don’t set the isFullVersion flag.  Also when you write the file, you never check to see if you successfully opened the file for writing either.

I would suggest some print statements to see what’s going on too.

Rob

@Rob, Thanks for your response

The ‘isFullVersion’ flag is set to false before entering the ‘loadPurchaseSettings’ function thus, in my opinion, eliminating the need to address most cases that will result in setting the flag to false.

In any case, the above point doesn’t address my scenario. I have to think about this further.

I would guess the file is getting corrupted for some reason (really old phones maybe?)… if so, the file would probably still exist, but the content would be messed up.

Maybe you should stop checking if the content is “true”, and just test if the file exists?

@marcior: exactly what i was thinking. 

Thanks