Make my app check for updates

Hello,

My first app-based game is playable, and I’m getting it ready to upload to the Google Store. Many commercial apps will prompt the user upon opening them “A new version is available. Would you like to download it from the Google Store?”

Is it worth the time to implement code to do this? Or will the Google Store automatically update the app to the new version for those players who allow automatic updates on their devices? If you think the first answer is better, which tutorial should I read to learn how to implement? 

Thanks a bunch for your help!

-Jeff

If user’s allow automatic updates, then GP should do that for you.  

For user’s who don’t allow them - you would need to have some PHP code or something similar on a server with the “latest” version number of your app. Then in your lua code, contact the server to get that version number and if it’s higher than the version number in the currently installed app redirect them to the appstore.

–myVersion.php

$event = array(); $event['response'] = "1.23.45"; echo $event;  

–main.lua

local currentVersionNumber = system.getInfo( "appVersionString" ) local function loadGame() --do your game loading stuff here end local function networkListener( event ) if event.response then if currentVersionNumber \< event.response then local function onComplete() system.openURL( "https://play.google.com/store/apps/details?id=com.yourcompany.yourappsname" ) end native.showAlert( "New version found", "Download latest version to continue", { "OK" }, onComplete ) else loadGame() end else loadGame() end end

If the version number in the game is 1.23.00, the code will get the latest version number from the server (1.23.45) and then redirect to GP. You would need to make sure the version number on the server is kept up to date (maybe there is a way to do that automatically).

If the version number on the device is the same or higher than the version on the server (or no response is found), then the game loads as normal. 

If you already have some server side stuff going on, then this is a fairly trivial thing to add but may help you in future.

Don’t forget, if you add this functionality to the game now, you don’t need to actually use it if you don’t need to. You could just keep the version number on the server as 1.00.00 or whatever your initial version number is and the if statement will always allow the game to load as normal.

However if you DON’T add something like this, then you can’t invoke it at a later date if you need to urgently force an update when the user opens the app (without adding it at a later date and hoping people update the app).

Alan QuizTix-

Thank you for the thorough and helpful response!

So you’ve convinced me to add a server-based version checker. My previous two games were both browser-based, and I had used APIs that automatically took care of this stuff for me. So I don’t yet have experience with keeping information on a server. Do you (or any other forum readers) have a recommendation for reading material to learn this aspect. I don’t mind googling, “How to keep info on a server,” but I’ll bet some of you know a better source.

Also, where in my code should I keep the version number within the game? The number that will be loaded from system.getInfo?

Thanks again! Have a great day!

If you build using the simulator (rather than using Corona Enterprise) then this:

system.getInfo( "appVersionString" )

will return the version number that you enter into the build window i.e. the second field in this image

corona_build.jpg

If this is the only piece of info you need to return from your server, then I wouldn’t overcomplicate matters with databases etc (although as soon as you need to start storing and managing multiple pieces of data then using a database will be a no-brainer.  

Just write the version number to a variable in the PHP script, and update that as needed. This is just a rough guide, so this probably won’t work exactly as is, but should hopefully give an idea of where to start:

–main.lua

local currentVersionNumber = system.getInfo( "appVersionString" ) local function loadGame() --do your game loading stuff here end local function networkListener( event ) if event.response then if currentVersionNumber \< event.response then local function onComplete() system.openURL( "https://play.google.com/store/apps/details?id=com.yourcompany.yourappsname" ) end native.showAlert( "New version found", "Download latest version to continue", { "OK" }, onComplete ) else loadGame() end else loadGame() end end network.request( "myServer.com/getLatestVersion.php?method=getLatestVersionNumber&OS="..system.getInfo("platformName"), "GET", networkListener, params )

YOURSERVER/classes/class_getLatestVersionNumber.php

\<?PHP class getLatestVersionNumber { /\*\* \* Constructor. \* \* \*/ function \_\_construct($args=NULL) { if(!empty($args)){ switch($args['method']){ case "getLatestVersionNumber": $this-\>\_getLatestVersionNumber( $args['OS'] ); break; default: //$this-\>\_returnRegisteredFriends(); echo "No quiz Task Provided...\n"; break; } } } private function \_getLatestVersionCatalog($OS){ $latestVersion = "unknown"; $isError = false; if($OS != "iPhone OS"){ $latestVersion = "1.0.2"; } elseif($OS != "Android"){ $latestVersion = "1.0.4"; } else{ $isError = true; } $return = array(); $return['response'] = $latestVersion; $return['isError'] = $isError; $return = json\_encode( $return); /\* Setting up JSON headers \*/ @header("content-type: text/json charset=utf-8"); echo $return; } }//end class ?\>

YOURSERVER/getLatestVersionNumber.php

\<?PHP // AUTOLOAD CLASS OBJECTS... YOU CAN USE INCLUDES IF YOU PREFER if(!function\_exists("\_\_autoload")){ function \_\_autoload($class\_name){ require\_once('classes/class\_'.$class\_name.'.php'); } } // FETCH $\_GET OR CRON ARGUMENTS TO AUTOMATE TASKS $args = (!empty($\_POST)) ? $\_POST:array('method'=\>$argv[1]); // CREATE getLatestVersionNunmber CLASS OBJECT, WITH ARGUMENTS $quiz = new getLatestVersionNumber($args); ?\>

The lua part should be fairly straight forward. 

The next part is a PHP class which simply returns the version number if the OS passed in as a param is either “iPhone OS” or “Android”. You would obviously need to do something so that your sim builds worked, but this is just an example so that you can see what happens if ‘nil’ or something incorrect gets passed in.

The 3rd part is the PHP file that your network call would actually try and reach, and this file in turn calls the class described above. I’m sure that it’s not necessary to use this 2 step PHP method, but I’ve always used it without any problems and no one likes change  :slight_smile:

You would put these files on your server. In this example, getLatestVersionNumber.php is just in the root directory. There is then a subdirectory called classes, and class_getLatestVersionNumber.php is in there.

Rob Miracle made a guide on using REST services in Corona some time ago, I’m sue that will explain the general processes slightly better: http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

Thanks again! Super helpful!

If user’s allow automatic updates, then GP should do that for you.  

For user’s who don’t allow them - you would need to have some PHP code or something similar on a server with the “latest” version number of your app. Then in your lua code, contact the server to get that version number and if it’s higher than the version number in the currently installed app redirect them to the appstore.

–myVersion.php

$event = array(); $event['response'] = "1.23.45"; echo $event; &nbsp;

–main.lua

local currentVersionNumber = system.getInfo( "appVersionString" ) local function loadGame() --do your game loading stuff here end local function networkListener( event ) if event.response then if currentVersionNumber \< event.response then local function onComplete() system.openURL( "https://play.google.com/store/apps/details?id=com.yourcompany.yourappsname" ) end native.showAlert( "New version found", "Download latest version to continue", { "OK" }, onComplete ) else loadGame() end else loadGame() end end

If the version number in the game is 1.23.00, the code will get the latest version number from the server (1.23.45) and then redirect to GP. You would need to make sure the version number on the server is kept up to date (maybe there is a way to do that automatically).

If the version number on the device is the same or higher than the version on the server (or no response is found), then the game loads as normal. 

If you already have some server side stuff going on, then this is a fairly trivial thing to add but may help you in future.

Don’t forget, if you add this functionality to the game now, you don’t need to actually use it if you don’t need to. You could just keep the version number on the server as 1.00.00 or whatever your initial version number is and the if statement will always allow the game to load as normal.

However if you DON’T add something like this, then you can’t invoke it at a later date if you need to urgently force an update when the user opens the app (without adding it at a later date and hoping people update the app).

Alan QuizTix-

Thank you for the thorough and helpful response!

So you’ve convinced me to add a server-based version checker. My previous two games were both browser-based, and I had used APIs that automatically took care of this stuff for me. So I don’t yet have experience with keeping information on a server. Do you (or any other forum readers) have a recommendation for reading material to learn this aspect. I don’t mind googling, “How to keep info on a server,” but I’ll bet some of you know a better source.

Also, where in my code should I keep the version number within the game? The number that will be loaded from system.getInfo?

Thanks again! Have a great day!

If you build using the simulator (rather than using Corona Enterprise) then this:

system.getInfo( "appVersionString" )

will return the version number that you enter into the build window i.e. the second field in this image

corona_build.jpg

If this is the only piece of info you need to return from your server, then I wouldn’t overcomplicate matters with databases etc (although as soon as you need to start storing and managing multiple pieces of data then using a database will be a no-brainer.  

Just write the version number to a variable in the PHP script, and update that as needed. This is just a rough guide, so this probably won’t work exactly as is, but should hopefully give an idea of where to start:

–main.lua

local currentVersionNumber = system.getInfo( "appVersionString" ) local function loadGame() --do your game loading stuff here end local function networkListener( event ) if event.response then if currentVersionNumber \< event.response then local function onComplete() system.openURL( "https://play.google.com/store/apps/details?id=com.yourcompany.yourappsname" ) end native.showAlert( "New version found", "Download latest version to continue", { "OK" }, onComplete ) else loadGame() end else loadGame() end end network.request( "myServer.com/getLatestVersion.php?method=getLatestVersionNumber&OS="..system.getInfo("platformName"), "GET", networkListener, params )

YOURSERVER/classes/class_getLatestVersionNumber.php

\<?PHP class getLatestVersionNumber { /\*\* \* Constructor. \* \* \*/ function \_\_construct($args=NULL) { if(!empty($args)){ switch($args['method']){ case "getLatestVersionNumber": $this-\>\_getLatestVersionNumber( $args['OS'] ); break; default: //$this-\>\_returnRegisteredFriends(); echo "No quiz Task Provided...\n"; break; } } } private function \_getLatestVersionCatalog($OS){ $latestVersion = "unknown"; $isError = false; if($OS != "iPhone OS"){ $latestVersion = "1.0.2"; } elseif($OS != "Android"){ $latestVersion = "1.0.4"; } else{ $isError = true; } $return = array(); $return['response'] = $latestVersion; $return['isError'] = $isError; $return = json\_encode( $return); /\* Setting up JSON headers \*/ @header("content-type: text/json charset=utf-8"); echo $return; } }//end class ?\>

YOURSERVER/getLatestVersionNumber.php

\<?PHP // AUTOLOAD CLASS OBJECTS... YOU CAN USE INCLUDES IF YOU PREFER if(!function\_exists("\_\_autoload")){ function \_\_autoload($class\_name){ require\_once('classes/class\_'.$class\_name.'.php'); } } // FETCH $\_GET OR CRON ARGUMENTS TO AUTOMATE TASKS $args = (!empty($\_POST)) ? $\_POST:array('method'=\>$argv[1]); // CREATE getLatestVersionNunmber CLASS OBJECT, WITH ARGUMENTS $quiz = new getLatestVersionNumber($args); ?\>

The lua part should be fairly straight forward. 

The next part is a PHP class which simply returns the version number if the OS passed in as a param is either “iPhone OS” or “Android”. You would obviously need to do something so that your sim builds worked, but this is just an example so that you can see what happens if ‘nil’ or something incorrect gets passed in.

The 3rd part is the PHP file that your network call would actually try and reach, and this file in turn calls the class described above. I’m sure that it’s not necessary to use this 2 step PHP method, but I’ve always used it without any problems and no one likes change  :slight_smile:

You would put these files on your server. In this example, getLatestVersionNumber.php is just in the root directory. There is then a subdirectory called classes, and class_getLatestVersionNumber.php is in there.

Rob Miracle made a guide on using REST services in Corona some time ago, I’m sue that will explain the general processes slightly better: http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

Thanks again! Super helpful!