Preference plugin implementation in iOS
Posted By : Pawanpreet Singh | 01-Jul-2014
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
- Download the zip file from the above link.
- 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}
- Open your project in xcode.
- 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”
- Select Root.plist file generated by adding settings.bundle in xcode.
- Under Preference Items remove the following Items
Now you are left with Group, Toggle Switch.
- Text Field
- Slider
- 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);
});
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.