Skip to content

Instantly share code, notes, and snippets.

@Driv4r
Last active June 14, 2016 23:42
Show Gist options
  • Save Driv4r/76a2301eb371a2f7de7cf784bd10a70d to your computer and use it in GitHub Desktop.
Save Driv4r/76a2301eb371a2f7de7cf784bd10a70d to your computer and use it in GitHub Desktop.
Using platform specific code in the PCL project by using an interface that each project instantiates as their native implementation. This example uses the iOS NSUserDefaults, but Android SharedPreferences would used for the Android implementation.
using Myproject.Interfaces; using Myproject.Pages; using System; using Xamarin.Forms; namespace Myproject { /// <summary> /// The main class that is used to initialze xamarin forms. /// </summary> public partial class App : Application { /// <summary> /// Gets or sets the UserPreferencesStore. /// </summary> public static IUserPreferencesStore UserPreferencesStore { get; private set; } public App(IUserPreferencesStore userPreferencesStore) { InitializeComponent(); //Usually uses an init method, but this allows earlier access. App.UserPreferencesStore = userPreferencesStore; //App.Current.MainPage = new NavigationPage(new MainPage()); } } }
using Foundation; using UIKit; namespace Myproject.iOS { // The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new App(new UserPreferences())); return base.FinishedLaunching(app, options); } } }
namespace Myproject.Interfaces
{
/// <summary>
/// Interface the native projects must implement to access persistent storage on each platform.
/// </summary>
public interface IUserPreferencesStore
{
/// <summary>
/// Gets a string from persistent storage.
/// </summary>
/// <param name="key">The associated key.</param>
/// <returns>Returns the associated string.</returns>
string GetString(string key);
/// <summary>
/// Sets a string in persistent storage.
/// </summary>
/// <param name="key">The key to associate the string with.</param>
/// <param name="value">The string to save.</param>
void SetString(string key, string value);
/// <summary>
/// Gets a boolean from persistent storage.
/// </summary>
/// <param name="key">The associated key.</param>
/// <returns>Returns the associated boolean.</returns>
bool GetBool(string key);
/// <summary>
/// Sets a boolean in persistent storage.
/// </summary>
/// <param name="key">The key to associate the boolean with.</param>
/// <param name="value">The boolean to save.</param>
void SetBool(string key, bool value);
/// <summary>
/// Clears the persistent storage.
/// </summary>
void Reset();
}
}
using Myproject.Interfaces;
using Foundation;
namespace Myproject.iOS
{
/// <summary>
/// Native iOS implementation of the IUserPreferencesStore giving access to stored, serializable data.
/// </summary>
public class UserPreferencesiOS : IUserPreferencesStore
{
public string GetString(string key)
{
return NSUserDefaults.StandardUserDefaults.StringForKey(key);
}
public void SetString(string key, string value)
{
NSUserDefaults.StandardUserDefaults.SetString(value, key);
}
public bool GetBool(string key)
{
return NSUserDefaults.StandardUserDefaults.BoolForKey(key);
}
public void SetBool(string key, bool value)
{
NSUserDefaults.StandardUserDefaults.SetBool(value, key);
}
public void Reset()
{
NSUserDefaults.StandardUserDefaults.RemovePersistentDomain(NSBundle.MainBundle.BundleIdentifier);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment