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?
This document compares the different methods of sharing code between cross-platform projects: . NET Standard, Shared Projects, and Portable Class Libraries, including the benefits and disadvantages of each. There are three methods for sharing code between cross-platform applications: .
On Android, Xamarin's compiler assembles down to Intermediate Language (IL), which is then Just-in-Time (JIT) gathered to native assembly when the application takes-off.
Xamarin. Forms is a library of APIs that allows developers to build native apps for Android, iOS, and Windows completely in C#. Perhaps the most compelling feature of Xamarin. Forms is its support for native UI - you code the user interface of your application using Xamarin. Forms-specific forms and controls.
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); } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With