Write device platform specific code in Xamarin.Forms

35,021

Solution 1

This is a scenario which is easily resolved with dependency injection.

Have a interface with the desired methods on your shared or PCL code, like:

public interface IUserPreferences 
{
    void SetString(string key, string value);
    string GetString(string key);
}

Have a property on your App class of that interface:

public class App 
{
    public static IUserPreferences UserPreferences { get; private set; }

    public static void Init(IUserPreferences userPreferencesImpl) 
    {
        App.UserPreferences = userPreferencesImpl;
    }

    (...)
}

Create platform-specific implementations on your target projects:

iOS:

public class iOSUserPreferences : IUserPreferences 
{
    public void SetString(string key, string value)
    {
        NSUserDefaults.StandardUserDefaults.SetString(key, value);
    }

    public string GetString(string key)
    {
        (...)
    }
}

Android:

public class AndroidUserPreferences : IUserPreferences
{
    public void SetString(string key, string value)
    {
        var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
        var prefsEditor = prefs.Edit();

        prefEditor.PutString(key, value);
        prefEditor.Commit();
    }

    public string GetString(string key)
    {
        (...)
    }
}

Then on each platform-specific project create an implementation of IUserPreferences and set it using either App.Init(new iOSUserPrefernces()) and App.Init(new AndroidUserPrefernces()) methods.

Finally, you could change your code to:

public class MyPage : ContentPage
{
    public MyPage()
    {
        //do work to initialize MyPage 
    }

    public void LogIn(object sender, EventArgs eventArgs)
    {
        bool isAuthenticated = false;
        string accessToken = string.Empty;

        //do work to use authentication API to validate users

        if(isAuthenticated)
        {
            App.UserPreferences.SetString("AccessToken", accessToken);
        }
    }
}

Solution 2

Xamarin.Forms 2.3.4 introduced a new method for this:

if (Device.RuntimePlatform == Device.Android)
{
    // Android specific code
}
else if (Device.RuntimePlatform == Device.iOS)
{
    // iOS specific code
}
else if (Device.RuntimePlatform == Device.UWP)
{
    // UWP specific code
}

There are also other platforms to choose from, you can type in Device. in Visual Studio and it will show you the options.

Solution 3

There are multiple answers, depending on what you want to achieve, and the kind of project you have:

Execute different Xamarin.Forms code on different platforms.
Use this e.g. if you want different font sizes on different platforms:

label.Font = Device.OnPlatform<int> (12, 14, 14);

Execute platform specific code in a shared (PCL) project The common pattern is to use DI (dependency injection) for this. Xamarin.Forms provides a simple DependencyService for this, but use whatever you want.

Execute platform specific code in shared (Shared Asset Project) project As the code is compiled per platform, you can wrap your platform specific code in #if __PLATFORM__ #endif and have all the code in the same file. The platform project should define __IOS__, __ANDROID__ and __WINDOWS_PHONE__. Note that a shared asset project containing Xaml and code won't work well for iOS on Xamarin.Studio, and that having compiler directives makes your code harder to read and to test.

Solution 4

This seems less about Xamarin.Forms and more about using defaults in a PCL. Check out James Montemagno's github repo for doing cross-platform defaults.

Then just call his static methods for setting/retrieving. The nuget package is Xam.Plugins.Settings.

It can be used like this:

using Refractored.Xam.Settings;

...

CrossSettings.Current.AddOrUpdateValue("AccessToken", accessToken);
var value = CrossSettings.Current.GetValueOrDefault<string>("AccessToken");

Solution 5

Xamarin.Forms has a built-in dependency injector if you take a look at their guide in the developer area of their website (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/)

There's also a wonderful library you can pull from NuGet/Github (https://github.com/aritchie/acr-xamarin-forms) that will handle the storage requirement you are looking for... take a look at the Settings service in there, it will even handle serialization of more complex objects.

Share:
35,021
Michael Kniskern
Author by

Michael Kniskern

I am currently working as an IT engineer for the government of Mesa, Arizona USA

Updated on June 10, 2020

Comments

  • Michael Kniskern
    Michael Kniskern almost 4 years

    I have the following Xamarin.Forms.ContentPage class structure

    public class MyPage : ContentPage
    {
        public MyPage()
        {
            //do work to initialize MyPage 
        }
    
        public void LogIn(object sender, EventArgs eventArgs)
        {
            bool isAuthenticated = false;
            string accessToken = string.Empty;
    
            //do work to use authentication API to validate users
    
            if(isAuthenticated)
            {
                //I would to write device specific code to write to the access token to the device
                //Example of saving the access token to iOS device
                NSUserDefaults.StandardUserDefaults.SetString(accessToken, "AccessToken");
    
                //Example of saving the access token to Android device
                var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
                var prefsEditor = prefs.Edit();
    
                prefEditor.PutString("AccessToken", accessToken);
                prefEditor.Commit();
            }
        }
    }
    

    I would like to write platform specific code in the MyPage LogIn method to save the access token based on which device OS they are using my application on.

    How do I only run device specific code when the user uses my application on their device?