Google How to store user preferences in Xamarin.iOS? | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Tuesday, 13 March 2018

How to store user preferences in Xamarin.iOS?

Preferences are pieces of information that you store persistently and use to configure your app. Most preferences are stored locally using the Cocoa preferences system known as the user defaults system. This article covers working with NSUserDefault to save default settings in a Xamarin iOS App or Extension.

  • This article source code was prepared by using Visual Studio Community for Mac (7.3.2). And it is better to install latest visual studio updates from here.
  • This sample project is Xamarin.iOS project and tested in iOS simulator.
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.

And this article can explain you below topics:
1. How to create new Xamarin.iOS project?
2. How to store string value using NSUserDefaults?
3. How to read string value from NSUserDefaults Key?

1. Create the new Xamarin.iOS project. 
  • Launch Visual Studio for Mac.
  • On the File menu, select New Solution.
  • The New Project dialog appears. The left pane of the dialog lets you select the type of templates to display. In the left pane, expand iOS App General > Single View App and click on Next.
  • Enter your App Name (Ex: UserDefaultsSample), select minimum iOS support that you want to support and click on Next button.

  • You can choose your project location like below.

  • After that your project will load like below

2. How to store string value using NSUserDefaults?
Below is the sample code demonstrating the storing string data in Xamarin iOS.
  1. void SaveUserName(string key, string value){  
  2.     var plist = NSUserDefaults.StandardUserDefaults;  
  3.     plist.SetString(value, key);   
  4.     plist.Synchronize();  
Note: Apple no longer suggest that the developer call the Synchronize method to sync the in-memory cache with the database directly. Instead, it will be automatically called at periodic intervals to keep the in-memory cache in sync with a user’s defaults database.

Also if you want to store bool, integer data. You can write below code.
  1. NSUserDefaults.StandardUserDefaults.SetInt(2, "IntUniqueKey"); // int value store 
  2. NSUserDefaults.StandardUserDefaults.SetBool(false"BooleanUniqueKey"); //bool value store   
3. How to read string value from NSUserDefaults Key?
  1. string ReadUserName( ){  
  2.       return NSUserDefaults.StandardUserDefaults.StringForKey(UserNameKey);  
  3.   }  

Open ViewController class file and add below code.
  1. using System;  
  2. using Foundation;  
  3. using UIKit;  
  5. namespace UserDefaultsSample  
  6. {  
  7.     public partial class ViewController : UIViewController  
  8.     {  
  9.         public static string UserNameKey = "user_name";  
  11.         protected ViewController(IntPtr handle) : base(handle)  
  12.         {  
  13.             // Note: this .ctor should not contain any initialization logic.  
  14.         }  
  16.         public override void ViewDidLoad()  
  17.         {  
  18.             base.ViewDidLoad();  
  20.             SaveUserName(UserNameKey, "Subramanyam");  
  22.             var readUserName = ReadUserName();  
  23.         }  
  25.         public override void DidReceiveMemoryWarning()  
  26.         {  
  27.             base.DidReceiveMemoryWarning();  
  28.             // Release any cached data, images, etc that aren't in use.  
  29.         }  
  31.         void SaveUserName(string key, string value){  
  32.             var plist = NSUserDefaults.StandardUserDefaults;  
  33.             plist.SetString(value, key);   
  34.             plist.Synchronize();  
  35.         }  
  37.         string ReadUserName( )  
  38.         {  
  39.             return NSUserDefaults.StandardUserDefaults.StringForKey(UserNameKey);  
  40.         }  
  41.     }  
  42. }  

You can directly work on below sample source code to understand this article sample


FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? Otherwise, it would be very happy, if you have any thoughts for to implement this requirement in any other way? I always welcome if you drop comments on this post and it would be impressive.

Follow me always at @Subramanyam_B
Have a nice day by  :)

No comments:

Post a Comment

Search Engine Submission - AddMe