SlideShare a Scribd company logo
Application Settings
 and User Defaults
  - Parimal Satyal (realityequation.net)
                     October 28, 2009




     Based on Chapter 10 ‘Application Settings and User Defaults’
   Apress Beginning iPhone 3 Development: Exploring the iPhone SDK
                     by Dave Mark and Jeff LaMarch
Prefs

Most Mac apps have ‘Preferences’
Persistent Data/Settings
Settings app on iPhone OS
UI is built for you, uses plist
Settings
Apps can have own
tab in the Settings app
When in-app, when in
Settings app?
‘Backdoor’ prefs that
don’t need changing
Else, users must quit
UPrefs: Utility App
Utility App Goodness
Free UI
Free ‘flip side
view’ (animated)
Custom UIView
subclasses:
 MainView
 FlipsideView
iPhone Dev: Application Settings and Defaults
Settings Bundle

‘Settings Bundle’ to provide the data
Each bundle must have Root.plist
Additional views through
additional plists as child views
in Root.plist
New File > Resource
Root.plist Format
Root node is always a dictionary
(so must have key and value)
Children nodes can be:
 Dictionaries • capable of containing other data
 Arrays • capable of containing other data
 Boolean
 Data
 Number
 String

StringsTable for localization; ignore
PreferenceSpecifiers
PreferenceSpecifiers
PreferenceSpecifiers




-      Required: Type, Key, (Title)
PreferenceSpecifiers

               Key




                     Key




-      Required: Type, Key, (Title)
-       Key vs Key: Confusing
Adding Stuff
      and Grouping
Notice change in icons when expanded


PSGroupSpecifier signals new group
The contents of Root.plist determine
the UI and structure of Settings app
Other Inputs
Secure Text Field (isSecure key)
Multivalue field: PSMultiValueSpecifier
 Array as child, with separate
 Key list and Value list
Toggle Switch: PSToggleSwitchSpecifier
Slider: PSSliderSpecifier
Editing Root.plist
Editing Root.plist
Array to Drill Down
iPhone Dev: Application Settings and Defaults
Magic of Root.plist

Open Root.plist
Spend some time playing around and
seeing what changes what
Add another control:
A Toggle, a Slider or just a Text Field
Getting Settings data
 Easy, using the NSUserDefaults class
 NSUserDefaults *defaults =
 [NSUserDefaults standardUserDefaults];

 defaultsbecomes a dictionary, so we can
 do stuff like:
 (NSString *) objectForKey:
 (NSDate *) objectForKey:
 (int) intForKey:
 (BOOL) boolForKey:
UI: MainView.xib
MainViewController
Explore the .h and .m files
Take a look at these methods:
 refreshFields:
 flipSideViewControllerDidFinish:
 viewDidAppear:

Declaring constants, creating a method
Property-ize and synthesize elements
Map them using NSUserDefaults class &
objectForKey: method
MainViewController.m
   - (void) refreshFields {
	 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	 usernameLabel.text = [defaults objectForKey: kUsernameKey];
	 passwordLabel.text = [defaults objectForKey: kPasswordKey];
	 languageLabel.text = [defaults objectForKey: kLanguageKey];
	 beAwesomeLabel.text = [defaults objectForKey: kBeAwesomeKey];
	 awesomenessLabel.text = [[defaults objectForKey: kAwesomenessKey]
stringValue];
}

- (void) viewDidAppear:(BOOL)animated {
	 [self refreshFields];
	 [super viewDidAppear:animated];
}
MainViewController.m
   - (void) refreshFields {
	 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	 usernameLabel.text = [defaults objectForKey: kUsernameKey];
	 passwordLabel.text = [defaults objectForKey: kPasswordKey];
	 languageLabel.text = [defaults objectForKey: kLanguageKey];
	 beAwesomeLabel.text = [defaults objectForKey: kBeAwesomeKey];
	 awesomenessLabel.text = [[defaults objectForKey: kAwesomenessKey]
stringValue]; ‹ don’t forget stringValue
}

- (void) viewDidAppear:(BOOL)animated {
	 [self refreshFields];
	 [super viewDidAppear:animated];
}
Changing Defaults

Design UI
Create outlets
Use getters and setters to make the
changes
FlipsideView.xib
Get what’s there when view loads
FlipsideViewController.m
    - (void)viewDidLoad {

    [super viewDidLoad];

	 NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];

	 awesomeSwitch.on = ([[defaults objectForKey:kBeAwesomeKey]
isEqualToString:@"Oui"]) ? YES: NO;

	 awesomenessSlider.value = [defaults
floatForKey:kAwesomenessKey];

}
Set new values when moving out
FlipsideViewController.m
  - (void) viewWillDisappear:(BOOL)animated {

	 NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];

	 NSString *prefValue = (awesomeSwitch.on)? @"Oui" :
@"Non";

	 [defaults setObject:prefValue forKey:kBeAwesomeKey];
	 [defaults setFloat:awesomenessSlider.value
forKey:kAwesomenessKey];

	 [super viewWillDisappear:animated];
}
Finishing Up

Clean up with dealloc, viewDidUnload for
both Views
Fix any Build errors
And you get...
iPhone Dev: Application Settings and Defaults
iPhone Dev: Application Settings and Defaults

More Related Content

PDF
Drupal 8 Sample Module
PDF
Drupal 8: Forms
PDF
Any tutor
PDF
Drupal 8: Routing & More
PDF
Advanced Querying with CakePHP 3
PPTX
Mule esb – connecting to ms sql db
PDF
Drupal Module Development
Drupal 8 Sample Module
Drupal 8: Forms
Any tutor
Drupal 8: Routing & More
Advanced Querying with CakePHP 3
Mule esb – connecting to ms sql db
Drupal Module Development

What's hot (19)

PDF
New in cakephp3
PDF
Changes to Drupal Themes in version 7 (part 1)
PDF
Drupal 8: Entities
DOC
Quiz With Answers Drupal
PDF
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
PPTX
KEY
Introducing CakeEntity
KEY
Development Approach
PDF
Drupal vs WordPress
KEY
Introducing CakeEntity
PPTX
Modular javascript
PDF
Future of HTTP in CakePHP
PDF
Field api.From d7 to d8
PDF
CakeFest 2013 keynote
PPTX
Cake PHP 3 Presentaion
PDF
PostgreSQL (2) by Aswin
PDF
Ejb3 Struts Tutorial En
PPT
Php Data Objects
PDF
First java-server-faces-tutorial-en
New in cakephp3
Changes to Drupal Themes in version 7 (part 1)
Drupal 8: Entities
Quiz With Answers Drupal
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Introducing CakeEntity
Development Approach
Drupal vs WordPress
Introducing CakeEntity
Modular javascript
Future of HTTP in CakePHP
Field api.From d7 to d8
CakeFest 2013 keynote
Cake PHP 3 Presentaion
PostgreSQL (2) by Aswin
Ejb3 Struts Tutorial En
Php Data Objects
First java-server-faces-tutorial-en
Ad

Similar to iPhone Dev: Application Settings and Defaults (20)

PDF
PDF
Drupal 7 Entity & Entity API
PDF
KAAccessControl
PDF
Intake 37 ef2
PPT
PPTX
Drupal 7 entities & TextbookMadness.com
PDF
Android Data Persistence
PDF
13th Sep, Drupal 7 advanced training by TCS
PPTX
iOS Course day 2
PDF
Play 2.0
ZIP
First Steps in Drupal Code Driven Development
PPTX
Sencha Touch - Introduction
PPTX
Mobile Application Development (Shared Preferences) class-06
PDF
Angular.js Primer in Aalto University
PDF
Ejb3 Struts Tutorial En
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
ODP
Modular programming Using Object in Scala
PDF
Entities in drupal 7
ODP
Session 2- day 3
PPT
Pxb For Yapc2008
Drupal 7 Entity & Entity API
KAAccessControl
Intake 37 ef2
Drupal 7 entities & TextbookMadness.com
Android Data Persistence
13th Sep, Drupal 7 advanced training by TCS
iOS Course day 2
Play 2.0
First Steps in Drupal Code Driven Development
Sencha Touch - Introduction
Mobile Application Development (Shared Preferences) class-06
Angular.js Primer in Aalto University
Ejb3 Struts Tutorial En
Zend Framework 1.9 Setup & Using Zend_Tool
Modular programming Using Object in Scala
Entities in drupal 7
Session 2- day 3
Pxb For Yapc2008
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks

iPhone Dev: Application Settings and Defaults

  • 1. Application Settings and User Defaults - Parimal Satyal (realityequation.net) October 28, 2009 Based on Chapter 10 ‘Application Settings and User Defaults’ Apress Beginning iPhone 3 Development: Exploring the iPhone SDK by Dave Mark and Jeff LaMarch
  • 2. Prefs Most Mac apps have ‘Preferences’ Persistent Data/Settings Settings app on iPhone OS UI is built for you, uses plist
  • 3. Settings Apps can have own tab in the Settings app When in-app, when in Settings app? ‘Backdoor’ prefs that don’t need changing Else, users must quit
  • 5. Utility App Goodness Free UI Free ‘flip side view’ (animated) Custom UIView subclasses: MainView FlipsideView
  • 7. Settings Bundle ‘Settings Bundle’ to provide the data Each bundle must have Root.plist Additional views through additional plists as child views in Root.plist
  • 8. New File > Resource
  • 9. Root.plist Format Root node is always a dictionary (so must have key and value) Children nodes can be: Dictionaries • capable of containing other data Arrays • capable of containing other data Boolean Data Number String StringsTable for localization; ignore
  • 12. PreferenceSpecifiers - Required: Type, Key, (Title)
  • 13. PreferenceSpecifiers Key Key - Required: Type, Key, (Title) - Key vs Key: Confusing
  • 14. Adding Stuff and Grouping Notice change in icons when expanded PSGroupSpecifier signals new group The contents of Root.plist determine the UI and structure of Settings app
  • 15. Other Inputs Secure Text Field (isSecure key) Multivalue field: PSMultiValueSpecifier Array as child, with separate Key list and Value list Toggle Switch: PSToggleSwitchSpecifier Slider: PSSliderSpecifier
  • 20. Magic of Root.plist Open Root.plist Spend some time playing around and seeing what changes what Add another control: A Toggle, a Slider or just a Text Field
  • 21. Getting Settings data Easy, using the NSUserDefaults class NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; defaultsbecomes a dictionary, so we can do stuff like: (NSString *) objectForKey: (NSDate *) objectForKey: (int) intForKey: (BOOL) boolForKey:
  • 23. MainViewController Explore the .h and .m files Take a look at these methods: refreshFields: flipSideViewControllerDidFinish: viewDidAppear: Declaring constants, creating a method Property-ize and synthesize elements Map them using NSUserDefaults class & objectForKey: method
  • 24. MainViewController.m - (void) refreshFields { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; usernameLabel.text = [defaults objectForKey: kUsernameKey]; passwordLabel.text = [defaults objectForKey: kPasswordKey]; languageLabel.text = [defaults objectForKey: kLanguageKey]; beAwesomeLabel.text = [defaults objectForKey: kBeAwesomeKey]; awesomenessLabel.text = [[defaults objectForKey: kAwesomenessKey] stringValue]; } - (void) viewDidAppear:(BOOL)animated { [self refreshFields]; [super viewDidAppear:animated]; }
  • 25. MainViewController.m - (void) refreshFields { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; usernameLabel.text = [defaults objectForKey: kUsernameKey]; passwordLabel.text = [defaults objectForKey: kPasswordKey]; languageLabel.text = [defaults objectForKey: kLanguageKey]; beAwesomeLabel.text = [defaults objectForKey: kBeAwesomeKey]; awesomenessLabel.text = [[defaults objectForKey: kAwesomenessKey] stringValue]; ‹ don’t forget stringValue } - (void) viewDidAppear:(BOOL)animated { [self refreshFields]; [super viewDidAppear:animated]; }
  • 26. Changing Defaults Design UI Create outlets Use getters and setters to make the changes
  • 28. Get what’s there when view loads FlipsideViewController.m - (void)viewDidLoad { [super viewDidLoad]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; awesomeSwitch.on = ([[defaults objectForKey:kBeAwesomeKey] isEqualToString:@"Oui"]) ? YES: NO; awesomenessSlider.value = [defaults floatForKey:kAwesomenessKey]; }
  • 29. Set new values when moving out FlipsideViewController.m - (void) viewWillDisappear:(BOOL)animated { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *prefValue = (awesomeSwitch.on)? @"Oui" : @"Non"; [defaults setObject:prefValue forKey:kBeAwesomeKey]; [defaults setFloat:awesomenessSlider.value forKey:kAwesomenessKey]; [super viewWillDisappear:animated]; }
  • 30. Finishing Up Clean up with dealloc, viewDidUnload for both Views Fix any Build errors And you get...