How to Check the App is running for the First time – in Xamarin Forms (Android/iOS/Windows)

Objective

In one of my Xamarin Forms apps, I wanted to check is the app running for the first time after the installation or the app is already installed and according to that Load different pages in the app

 How to Install Xamarin in Visual Studio 2017…

Furthermore,

  1. If the App is running for the first time, I wanted to a Quick Start Guide to the user.
  2. If all has already installed, app will show the Login Form.

pags

Then I looked for a solution and James Montemagno‘s  SettingsPlugin gave me the solution…

If you want to learn more, Click above links to go to GitHub and  MotzCod.es.

How I did it ?

First I went through the Instruction of the above references and I installed the plugin using NuGet..

Here I’ll explain how I did it.. and Here is my project at the beginning.

1-project

1. First Install the Settings Plugin using NuGet packages.. Search  “Xam.Plugins.Settings”

2-download-settings

2. Apply The package to all projects or selected projects.. (But I’m managing the settings from the PCL project) and Install the package..

3-select-all-projects

3. Then Conform the installation..

4-conform-installa-tion

4. When the packages has installed, It will generate a folder which is “Helpers” and a class onside it “Settings.cs” to manage settings.

It will added to all selected projects.

5-helpers-settings

The default Settings class includes below C# code to manage settings..


using Plugin.Settings;
using Plugin.Settings.Abstractions;

namespace FirstTimeUse.Droid.Helpers
{
///
<summary>
/// This is the Settings static class that can be used in your Core solution or in any
/// of your client applications. All settings are laid out the same exact way with getters
/// and setters.
/// </summary>

public static class Settings
{
private static ISettings AppSettings
{
get
{
return CrossSettings.Current;
}
}

#region Setting Constants

private const string SettingsKey = "settings_key";
private static readonly string SettingsDefault = string.Empty;

#endregion
public static string GeneralSettings
{
get
{
return AppSettings.GetValueOrDefault<string>(SettingsKey, SettingsDefault);
}
set
{
AppSettings.AddOrUpdateValue<string>(SettingsKey, value);
}
}

}
}

To read the more about the Settings class go to:  https://github.com/jamesmontemagno/SettingsPlugin

I’m going to store my value in the SettingsKey property. So I’ll change the default value of the SettingsKey property to “yes”. Which means the app is running for the first time.

6-change-settings-key


private static readonly string SettingsDefault = "yes";

5. Now I’m going to use the Settings in the application..

I’m checking the app status in the App.xaml.cs class. Because the app navigating to the Main page happens in the constructor in App.xaml.cs class.

The default code of the App class is… And there are not properties in this class…


public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}

protected override void OnStart()
{
// Handle when your app starts
}

protected override void OnSleep()
{
// Handle when your app sleeps
}

protected override void OnResume()
{
// Handle when your app resumes
}
}

Now I’ll add a Property to App class to Manage App Settings. The property name is “IsFirstTime” and type is String.

7-add-isfirsttime


///
<summary>
/// Get and Set value to General settings
/// </summary>

public string IsFirstTime
{
get { return Settings.GeneralSettings; }
set
{
if (Settings.GeneralSettings == value)
return;
Settings.GeneralSettings = value;
OnPropertyChanged();
}
}

6. Now I’ll add another page to use for log in to system if the app has already installed.

Page name is “LoginPage.xaml”.

8-added-2-pages

7. Now Lets Write code in App.xaml.cs to check the app status…

The page is initialize in the constructor of the App class. So I’m going to check the status of the app inside of the constructor.

And Here is the code. In the Settings class, I have set the default value as “yes“. So the app runs for the first time, the value should be “yes“. Then  the MainPage page will open. And when the app ran for the first time, it  sets the “IsFirstTime value to “no“.

Then when the user run the app after the first time, it will load the second page which is Logging Page.

9-check-first-time


public App()
{
InitializeComponent();

// Check is the app running for the first time
if (IsFirstTime == "yes")
{
// if this is the first time, set it to "No" and load the
// Main Page ,which will show at the first time use
IsFirstTime = "no";
MainPage = new FirstTimeUse.MainPage();
}
else
{
// If this is not the first time,
// Go to the login page
MainPage = new FirstTimeUse.LoginPage();
}
}

8. The Build and run the app using emulator. I’m using my Samsung Galaxy J7 as the emulator. And the App was run  successfully.

screenshot_20170219-173522

First time it will open the Main Page

screenshot_20170219-173745

And When I run the app after the first time, It loads the Login page..

 

References…

https://github.com/jamesmontemagno/SettingsPlugin

https://components.xamarin.com/gettingstarted/settingsplugin

 

Thanks !

6 thoughts on “How to Check the App is running for the First time – in Xamarin Forms (Android/iOS/Windows)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s