Saturday, 7 January 2012

WP7 location services policy check

Lately I've been playing with Windows Phone 7 app development. I submitted several free apps to Marketplace and had no troubles getting the apps certified and published. Not until I created an app that uses Location Services. The app failed to pass the certification stage with the following comment:

Your application failed the Marketplace prohibited application policy check. Please review the above policy, notes below (if applicable), update and re-submit your application.

To be honest, up to that moment I haven't been paying enough attention to certification requirements. After I received that notification I dived into the details and realized that the policy for application that use location information is quite strict. This can be easily justified by privacy protection etc.

How to pass location services policy check?

Knowing that my application is missing something I decided to fix it. I started with a lecture of Microsoft official Application Policy description. I really recommend you familiarize yourself with those rules before you start any WP7 app development.

The requirements regarding location policy I didn't fulfill are covered in Section 2.7. Once you read those you have a theoretical understanding of what you need to do. However, how does it look like in practice? I wasn't able to find a full example of an application that did pass the certification. Instead, I only found some general advices that I followed.

Below are 3 changes that I introduced to my app to make it pass the certification process
  1. Notification message
    Add a notification message on application start that will inform the user that your app is using location services. Give the user a choice to not allow that. The sample C# code for that could look like this:
    // Check if 'allow' setting already set
    bool? allow = null;
    if (PhoneApplicationService.Current.State.ContainsKey("allow"))
    {
    allow = (bool)PhoneApplicationService.Current.State["allow"];
    }

    if (allow == null)
    {
    // 'allow' setting not set yet (i.e. it is first page load)
    // Display the confirmation question
    var result = MessageBox.Show(
    "This application uses your location. Do you wish " +
    "to give it permission to use your location?",
    "User Location Data",
    MessageBoxButton.OKCancel);

    // Save answer so you can access it on other pages
    allow = (result == MessageBoxResult.OK);
    PhoneApplicationService.Current.State["allow"] = allow;
    }

    if (allow)
    {
    // Initiate your app normally
    }
    else
    {
    // Display message about limited functionality of your app
    // Disable elements that can cause usage of location services
    }
  2. Additional setting
    Add an additional setting item to your app that will allow the user to turn access to location services on and off. If your app doesn't have settings page yet you need to create one. The setting can use a simple checkbox. It should also reuse the state information we saved on application start. XAML code for that could be as simple as that:
    and your code behind:
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
    // Set checkbox value on page load
    bool allow = false;
    if (PhoneApplicationService.Current.State.ContainsKey("allow"))
    {
    allow = (bool)PhoneApplicationService.Current.State["allow"];
    }
    ckbAllow.IsChecked = allow;
    }

    private void CheckBox_Click(object sender, RoutedEventArgs e)
    {
    // change the saved value when checkbox is clicked
    PhoneApplicationService.Current.State["allow"] =
    ((CheckBox)sender).IsChecked.Value;
    }
  3. Policy page
    The last thing left to do is to create an additional page that describes to the user what the app exactly does with the location information it gathers. Inform the user if you save it or send it somewhere etc. The policy page can be linked from the main app page or available via application menu. The latter could be implemented like that:
    // Main page constructor
    public MainPage()
    {
    InitializeComponent();

    // Add an Application Menu Bar
    ApplicationBar = new ApplicationBar();
    ApplicationBar.IsMenuEnabled = true;
    ApplicationBar.IsVisible = true;
    ApplicationBar.Opacity = 1.0;

    // Add Policy menu item
    ApplicationBarMenuItem policyItem =
    new ApplicationBarMenuItem("location services policy");
    policyItem.Click += new EventHandler(policy_Click);
    ApplicationBar.MenuItems.Add(policyItem);

    // Add other menu items e.g. Settings
    }

    void policy_Click(object sender, EventArgs e)
    {
    // Show the policy page called Policy.xaml
    this.NavigationService.Navigate(
    new Uri("/Policy.xaml", UriKind.Relative));
    }
That's it. The 3 changes above worked for me and my app passed the certification process. Good luck with your apps!

No comments: