Mobile Corner

Roaming Options for User Settings in Windows Phone Applications

Integrating with SkyDrive can help your Windows Phone application save a user's settings to the cloud, and retrieve them across multiple devices and platforms.

In my "Storing Windows Phone User Settings" column, I showed you how to persist settings for your Windows Phone application via the IsolatedStorageSettings class. The limitation with this implementation, however, is that when users switch phones, or reinstall the application, they will lose those settings.

To address these issues, I'll show you how to save the settings to the cloud and retrieve them. At this point, there's a choice to be made regarding the cloud storage you use. One option is that you supply the nominal amount of storage required to store the settings for each user of your application. This typically involves a small initial cost for cloud storage, but it could escalate based on the success of your application.

Working with SkyDrive
The other option is to save the settings to users' cloud storage accounts. All Windows Phone users have a Windows Live account, so you can be assured they also have a SkyDrive account. (SkyDrive is one of the cloud storage options that Microsoft has available.) It’s important to note that SkyDrive has terms and conditions that stipulate what applications can do. Specifically, it defines the file formats that can be used, and that applications obtain the user's consent before reading or writing to files in SkyDrive.

The first step in working with SkyDrive is to download and install the Live software development kit (SDK). It includes a SignInButton control, which you can use within your application and wrappers around the REST API for SkyDrive. (Note that because SkyDrive falls under the Windows Live brand, users will use their Windows Live ID to sign in). You’ll also need a Client ID for your application, which you can create via the Live Connect app management site.

All that's left is to add a reference to the Microsoft.Live and Microsoft.Live.Controls assemblies. Right-click your Windows Phone application in Solution Explorer. Select Add Reference. Then select the assemblies from the .NET tab, and add the SignInButton control to your page.

<StackPanel>
    <live:SignInButton ClientId="[your client id]"
                        Scopes="wl.basic wl.photos wl.skydrive wl.offline_access wl.signin
                           wl.skydrive_update"
                        Branding="Skydrive"
                        TextType="SignIn"
                        SessionChanged="SignInButton_SessionChanged" />
    <Button Content="Save to cloud" Click="SaveClick" />
    <Button Content="Retrieve from cloud" Click="RetrieveClick" />
</StackPanel>

You’ll need to insert your ClientId in the appropriate attribute on the SignInButton. Running this application will display the SignInButton shown in Figure 1. Clicking the SignInButton will prompt the user to log into Windows Live before returning to the application. The SignInButton should then read Sign out, as shown in the right image of Figure 1.


[Click on image for larger view.]
Figure 1. Signing in to Windows Live.

If you restart the application, you’ll notice that it remembers whether the user has signed into Windows Live. The session information is actually persisted to the IsolatedStorageSettings class, which I covered in my previous column.

Saving to the Cloud
After signing into Windows Live, you'll want to save the current settings to the cloud. In the XAML, there are three event handlers that you'll need to define. After the user has signed into Windows Live, the application hands off the Session information to the Settings class, so that it can be used to access the user's SkyDrive account. The save and retrieve events call the corresponding static methods on the Settings class.

private void SignInButton_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
    if (e.Session != null && e.Status == LiveConnectSessionStatus.Connected)
    {
        Settings.Session = e.Session;
    }
}

private void SaveClick(object sender, RoutedEventArgs e)
{
    Settings.SaveSettingsToCloud();
}

private void RetrieveClick(object sender, RoutedEventArgs e)
{
    Settings.RetrieveCloudSettings();
}

The first change to the Settings class is to add a Session property, which will be used to access the user’s SkyDrive account, as shown in Listing 1.

Saving the settings into SkyDrive is done in two stages: acquire a reference to the "documents" folder, and then write the current settings file into the cloud, as shown in Listing 2.

Retrieving the saved settings from the cloud is the same but in reverse. The only major difference is that you need to re-populate the IsolatedStorageSettings instance. Unfortunately, there's no "reload" method on the IsolatedStorageSettings class. You need to load the retrieved settings into memory, and then update the IsolatedStorageSettings instance. The full code is shown in Listing 3.

With this code, you can save and retrieve application settings from the user’s SkyDrive account. You may decide to use a different cloud storage provider, in which case, you'll need to implement the logic for authenticating, and then saving and retrieving the application settings to the storage account.

Sharing Settings on Different Devices
One extension to this concept is to be able to share settings across applications that run on different devices. This includes a Windows Phone application running across multiple devices or different platforms, or when the user transfers to a new phone. For example, you may want users to be able to synchronize their settings between a Windows Phone application and a Windows 8 application. Windows 8 has its own built-in concept of roaming settings. It doesn’t expose an open API, which means you can’t use it to synchronize settings to other device types. The strategy outlined here will allow the user to share settings between any device types.

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube