×
Namespaces

Variants
Actions
Revision as of 02:51, 6 November 2013 by hamishwillee (Talk | contribs)

Application update prompt using Cimbalino Windows Phone Toolkit

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to create a simple 'Check for updates' prompt that checks the user has the latest version of your application installed

WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 8.0 SDK
Devices(s): Nokia Lumia 820, Nokia Lumia 920
Compatibility
Platform(s):
Windows Phone 8
Dependencies: Cimbalino Windows Phone Toolkit
Article
Created: I Jones (25 Oct 2013)
Last edited: hamishwillee (06 Nov 2013)

Contents

Introduction

This article demonstrates how to create a "Check for updates" prompt for an application. This is useful because it encourages users to update their apps even if they've ignored system notifications to do so; thereby reducing the number of people using older, less functional, and less stable versions of your app.

The code example compares the installed version of the application to the version currently available on the marketplace. If there is a newer version available, the user will be prompted and taken to the marketplace page for the application. The example has additional code to determine whether the user has already been prompted to update the app, and to control the rate at which they should be reminded if they chose to ignore an update request.

Check for update screenshot.png

Note.pngNote: The article Checking for updates from inside a Windows Phone app explains from first principles how the Cimbalino Windows Phone Toolkit determines whether an update is required.


Prerequisites

The only prerequisite for this example is you'll need to install the excellent "Cimbalino Windows Phone Toolkit". This can be done using nuget or can be downloaded from here (for more information see How to install Cimbalino Windows Phone Toolkit packages).


Setting up the manifest file

There are a few things that you'll need to set up in your application's manifest file. Firstly you'll need to ensure that the ProductID in the manifest matches the App ID of your published application (this can be found on the details tab for your application on the Windows Phone Developer Dashboard portal). You also need to make sure that you update the version number in you manifest every time you update the application.


Create the Update Reminder Class

The ApplicationUpdateReminder class acts as a wrapper for the services used to check the marketplace for updates, the prompt, and the code that defines the re-prompting behaviour.

Add this class to your Windows Phone Project.

 public class ApplicationUpdateReminder
{
 
public string CurrentVersion { get; set; }
public int? RecurrencePerUsageCount { get; set; }
public string MessagePrompt { get; set; }
public string MessageTitle { get; set; }
 
public int UpdateRejectedCount
{
get
{
return _rejectCount;
}
}
 
private int _usageCount = 0;
private int _rejectCount = 0;
private string _MessagePrompt;
private string _MessageTitle;
private const string LAUNCH_COUNT = "LAUNCH_COUNT";
private const string REJECT_UPDATE_COUNT = "REJECT_UPDATE_COUNT";
private const string MESSAGE_PROMPT = "Do you want to install the new version now?";
private const string MESSAGE_TITLE = "Update Available";
 
public ApplicationUpdateReminder()
{
LoadState();
}
 
private void LoadState()
{
this._usageCount = AppSettings.Read<int>(LAUNCH_COUNT, 0);
_usageCount++;
 
this._rejectCount = AppSettings.Read<int>(REJECT_UPDATE_COUNT, 0);
 
}
 
private void StoreState()
{
AppSettings.Write(LAUNCH_COUNT, _usageCount);
 
AppSettings.Write(REJECT_UPDATE_COUNT, _rejectCount);
}
 
public void Notify()
{
if (RecurrencePerUsageCount.HasValue)
{
if (_usageCount >= RecurrencePerUsageCount.Value)
{
_usageCount = 0; //reset because check has been fired;
CheckForUpdates();
}
}
else
{
CheckForUpdates();
}
 
StoreState();
}
 
 
private async void CheckForUpdates()
{
try
{
var _informationService = new MarketplaceInformationService();
var _applicationManifestService = new ApplicationManifestService();
var result = await _informationService.GetAppInformationAsync();
var appInfo = _applicationManifestService.GetApplicationManifest();
Version myVersion;
 
if (String.IsNullOrEmpty(CurrentVersion))
{
myVersion = new Version(appInfo.App.Version);
}
else
{
myVersion = new Version(CurrentVersion);
}
 
 
var updatedVersion = new Version(result.Entry.Version);
 
 
if (String.IsNullOrEmpty(MessageTitle))
{
_MessageTitle = MESSAGE_TITLE;
}
else
{
_MessageTitle = MessageTitle;
}
 
 
if (String.IsNullOrEmpty(MessagePrompt))
{
_MessagePrompt = MESSAGE_PROMPT;
}
else
{
_MessagePrompt = MessagePrompt;
}
 
 
if (updatedVersion > myVersion)
{
if (MessageBox.Show(_MessagePrompt, _MessageTitle, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
new MarketplaceDetailTask().Show();
}
else
{
_rejectCount++;
}
 
}
}
catch (Exception ex)
{
//Log the error somehow
 
}
}
 
public void ForceCheckForUpdates()
{
CheckForUpdates();
}
}

Setting up the reminder class

We now need to setup the reminder class for use. Firstly add a public property for the ApplicationUpdateReminder class in App.xaml.cs:

 public ApplicationUpdateReminder updateReminder;

Then add the following code to the App class constructor:

  updateReminder = new ApplicationUpdateReminder();
updateReminder.RecurrencePerUsageCount = 3;

This will check for updates every third time the application is run.


Checking for updates

The code for checking your app is now quite straightforward. In your page constructor, add one of the following code snippets:

  //this will use the RecurrencePerUsageCount
(App.Current as App).updateReminder.Notify();
 
// this will ignore RecurrencePerUsageCount
(App.Current as App).updateReminder.ForceCheckForUpdates();


Downloads

You can download the source code for this example here File:CheckForUpdatesSource.zip

Summary

This is an easy way to ensure that all users are running the latest version of your app and can really help with reviews like "the app is broke" when users are running older versions of your app. There is also an UpdateRejectedCount property that you could use to disable the application after a specified number of update rejections if you wished.

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

488 page views in the last 30 days.
×