Preference plugin implementation in iOS

Posted By : Pawanpreet Singh | 01-Jul-2014

Preference plugin Implementation in iOS

Here I will show you how one can add his cordova application in device settings and can handle some events through it.

Plugin to use:

I used plugin named cordova ios application preference you can use the following link to check the plugin at Github

https://github.com/escio/cordova-ios-application-preferences

To add this plugin in your project follow the following procedure

  1. Download the zip file from the above link.
  2. Using terminal go in your project directory, from inside your cordova project you have to enter the following command
     cordova plugin add {path to downloaded plugin}
     
  3. Open your project in xcode.
  4. Go to File > New > File... then a new window will open select select resource in iOS then select Settings Bundle.

Now a new settings.bundle file will be added to your project

Visit this link for more detail about settings.bundle

Now I will show you how to use this plugin.

Change the value of “Group” to “Clear local data of app in next startup”

 

Remove “Name” and “none given” key/value pair.

Change the value of “Enabled” to “Clear”

  1. Select Root.plist file generated by adding settings.bundle in xcode.
  2. Under Preference Items remove the following Items

    Now you are left with Group, Toggle Switch.

    • Text Field
    • Slider
  3. In Root.strings file

Note the name “enabled_preference” is the Identifier of your Toggle switch in Root.plist file

Open your application js file

Add this method to check the value of the toggle switch

window.plugins.applicationPreferences.get('enabled_preference', function(result) {
        alert("Current Toggle State: " + result);
    }, function(error) {
        alert("There was some issue getting value: " + error);
    }
);

and the following method to set the value of toggle switch

window.plugins.applicationPreferences.set('enabled_preference',0, function() {
				    
				    }, function(error) {
				        console.log("Failed to retrieve a setting: " + error);
				});

We are setting the value 0 to turn the toggle to off(false) state

 

If you are storing any local data you can clear it using the value of Toggle Switch

You can set the default value of Toggle Switch to ‘NO’ in Root.plist file

If a user changes its value to Yes on next startup of the app You can check its value then clear the local data and then set the value back to No using the following code.

window.plugins.applicationPreferences.get('enabled_preference', function(result) {
       		if(parseInt(result) == 1){
       			localStorage.clear();
       			window.plugins.applicationPreferences.set('enabled_preference',0, function() {
				    
				    }, function(error) {
				        console.log("There was some error: " + error);
				});

About Author

Author Image
Pawanpreet Singh

Pawanpreet is an seasoned Project Manager with a wealth of knowledge in software development, specializing in frontend and mobile applications. He possesses a strong command of project management tools, including Jira, Trello, and others. With a proven track record, he has successfully overseen the delivery of multiple software development projects, managing budgets and large teams. Notable projects he has contributed to include TimeForge, Yogyata, Kairos, Veto, Inspirien App, and more. Pawanpreet excels in developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within allocated resources. He collaborates closely with clients to define project scope and requirements, establish timelines and milestones, and effectively manage expectations. Regular project status meetings are conducted by him, providing clients and stakeholders with consistent updates on project progress, risks, and issues. Additionally, he coaches and mentors project leads, offering guidance on project management best practices and supporting their professional development.

Request for Proposal

Name is required

Comment is required

Sending message..