Saving user preferences in iOS using Xamarin

Introduction:
Xamarin provides a number of ways to save data in iOS using Local Storage. User preferences are generally stored in Library/Preferences/.
Xamarin iOs provides its built in mechanism i.e. instead of creating files directly in the directory it is suggested to use NSUserDefaults class to save data.

Description:
NSUserDefaults class provides its own built in mechanisms to save float, double, integers, Boolean and URL data. To save a custom object it should be converted to NSData object using NSUserDefaults.

Below is the sample code demonstrating the string, Boolean and integer data in Xamarin iOS respectively.

Saving a string :

NSUserDefaults.StandardUserDefaults.SetString("Hi My test String", "StringUniqueKey"); 

Saving a boolean object :

NSUserDefaults.StandardUserDefaults.SetBool(true, "BooleanUniqueKey"); 

Saving an integer :

NSUserDefaults.StandardUserDefaults.SetInt(100, "IntUniqueKey"); 

Conclusion:
NSUserDefaults.StandardUserDefaults gives us access to the preferences folder and we can call respective methods to save the data. This mechanism provides easy mode to save float, double, integer, Boolean and URL data that can be reused over the time.

Written By: Anobik Dey, Software Developer, Mindfire Solutions

Advertisements

One thought on “Saving user preferences in iOS using Xamarin

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