Posted By

espinallab on 03/04/10


Tagged

read write user and preferences


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

guayo
umang_nine
avanadra
clearbrian
sb
nsbuckner
im3der
wpellis
Tintenklecks
ringo380
code4mac
cnathan


Read and Write User Preferences


 / Published in: Objective C
 

URL: http://iphonedevelopertips.com/cocoa/read-and-write-user-preferences.html

This is really cool, thanks to the author for putting it together.

  1. //
  2. // SetPrefsAppDelegate.h
  3. //
  4. #import <UIKit/UIKit.h>
  5.  
  6. @interface SetPrefsAppDelegate : NSObject <UIApplicationDelegate>
  7. {
  8. BOOL saveUsername;
  9. NSInteger preferredIndexInTabbar;
  10. UIWindow *window;
  11. }
  12.  
  13. @property (nonatomic, retain) IBOutlet UIWindow *window;
  14.  
  15. @end
  16.  
  17.  
  18. // SetPrefsAppDelegate.m
  19. //
  20. #import "SetPrefsAppDelegate.h"
  21. #import "Preferences.h"
  22.  
  23. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. // App Delegate implementation
  25. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. @implementation SetPrefsAppDelegate
  27.  
  28. @synthesize window;
  29.  
  30. /*------------------------------------------------------
  31. * Read preferences from system
  32. *-----------------------------------------------------*/
  33. -(void) loadPreferences
  34. {
  35. saveUsername = [Preferences shouldSaveUsername];
  36. preferredIndexInTabbar = [Preferences startupTab];
  37. }
  38.  
  39. /*------------------------------------------------------
  40. * Application startup code
  41. *-----------------------------------------------------*/
  42. - (void)applicationDidFinishLaunching:(UIApplication *)application
  43. {
  44. // Load user preferences (notice the default values the first time through)
  45. [self loadPreferences];
  46.  
  47. // Show the current values of the preferences
  48. NSLog(@"Save user preferences: %s", saveUsername == YES ? "Yes" : "No");
  49. NSLog(@"Preferred startup tab: %d", preferredIndexInTabbar);
  50.  
  51. // This is a little contrived, but you get the point...
  52. BOOL saveUname = YES;
  53. NSInteger index = 3;
  54.  
  55. // Write new values to the sytem
  56. [Preferences setPreferences:saveUname startupTab:index];
  57.  
  58. // NSLog(@"Home: %s", NSHomeDirectory());
  59. // NSLog(@"Home: %@", NSHomeDirectory());
  60.  
  61. // Override point for customization after application launch
  62. [window makeKeyAndVisible];
  63. }
  64.  
  65. - (void)dealloc
  66. {
  67. [window release];
  68. [super dealloc];
  69. }
  70.  
  71. @end

Report this snippet  

You need to login to post a comment.