Your SlideShare is downloading. ×
iPhone Dev: Application Settings and Defaults
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

iPhone Dev: Application Settings and Defaults

2,003
views

Published on

Learn how to let users change settings and defaults for your application through Apple's 'Settings' app. Also shows you how to retrieve and change these settings from elsewhere in the …

Learn how to let users change settings and defaults for your application through Apple's 'Settings' app. Also shows you how to retrieve and change these settings from elsewhere in the application.

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

Published in: Technology, News & Politics

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,003
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Transcript

    • 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
    • 4. UPrefs: Utility App
    • 5. Utility App Goodness Free UI Free ‘flip side view’ (animated) Custom UIView subclasses: MainView FlipsideView
    • 6. 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
    • 7. New File > Resource
    • 8. 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
    • 9. PreferenceSpecifiers
    • 10. PreferenceSpecifiers
    • 11. PreferenceSpecifiers - Required: Type, Key, (Title)
    • 12. PreferenceSpecifiers Key Key - Required: Type, Key, (Title) - Key vs Key: Confusing
    • 13. 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
    • 14. 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
    • 15. Editing Root.plist
    • 16. Editing Root.plist
    • 17. Array to Drill Down
    • 18. 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
    • 19. 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:
    • 20. UI: MainView.xib
    • 21. 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
    • 22. 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]; }
    • 23. 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]; }
    • 24. Changing Defaults Design UI Create outlets Use getters and setters to make the changes
    • 25. FlipsideView.xib
    • 26. 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]; }
    • 27. 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]; }
    • 28. Finishing Up Clean up with dealloc, viewDidUnload for both Views Fix any Build errors And you get...