iPhone Dev: Application Settings and Defaults

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    iPhone Dev: Application Settings and Defaults - Presentation 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...

    + livatlantislivatlantis, 1 month ago

    custom

    114 views, 0 favs, 0 embeds more stats

    Learn how to let users change settings and defaults more

    More info about this document

    CC Attribution License

    Go to text version

    • Total Views 114
      • 114 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories