Application Setting in iPhone

In this application we will see how to  display your app’s preferences or settings in the “Settings” system application.  A settings bundle, included in your application’s bundle directory, contains the information needed by the Settings application to display your preferences and make it possible for the user to modify them.

Step 1: Create a Window base application using template. Give the name of the application “ApplicationSetting”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Now we’ll add table view controller class to the project. Choose New file -> Select cocoa touch classes group and then select UITableViewController subclass. Give the name “ApplicationSettingViewController”.

Step 4: In our application we need to add Settings.bundle , so select ApplicationSetting -> Add -> New File -> Resources -> Setting Bundle , and give the name settings. Then your will see there are two things are created , 1. Root.plist, 2. Root.strings.

Step 5: In the AppDelegate.h file, we will impliment the NavigationController in the file. Make the following changes in the file.

  enum textColors
   blue = 1,

 enum backgroundColors
    blackBackColor = 1,
@interface ApplicationSettingAppDelegate : NSObject  {
    UIWindow *window;
    UINavigationController	*navigationController;

   NSString	*firstName;
   NSString	*lastName;
   NSInteger	backgroundColor;
   NSInteger	textColor;
  @property (nonatomic, retain) IBOutlet UIWindow *window;
  @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

  @property (nonatomic, retain) NSString *firstName;
  @property (nonatomic, retain) NSString *lastName;
  @property (nonatomic, assign) NSInteger backgroundColor;
  @property (nonatomic, assign) NSInteger textColor;

Step 6: Make the following changes in the AppDelegate.m file.

 - (void)applicationDidFinishLaunching:(UIApplication *)application
   [window addSubview:[navigationController view]];

   NSString *testValue = [[NSUserDefaults standardUserDefaults]    stringForKey:kFirstNameKey];
 if (testValue == nil)
    NSString *pathStr = [[NSBundle mainBundle] bundlePath];
    NSString *settingsBundlePath = [pathStr    stringByAppendingPathComponent:@"Settings.bundle"];
   NSString *finalPath = [settingsBundlePath     stringByAppendingPathComponent:@"Root.plist"];

   NSDictionary *settingsDict = [NSDictionary   dictionaryWithContentsOfFile:finalPath];
   NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];

  NSString *firstNameDefault;
  NSString *lastNameDefault;
  NSNumber *nameColorDefault;
  NSNumber *backgroundColorDefault;

  NSDictionary *prefItem;
 for (prefItem in prefSpecifierArray)
   NSString *keyValueStr = [prefItem objectForKey:@"Key"];
   id defaultValue = [prefItem objectForKey:@"DefaultValue"];

 if ([keyValueStr isEqualToString:kFirstNameKey])
    firstNameDefault = defaultValue;
 else if ([keyValueStr isEqualToString:kLastNameKey])
    lastNameDefault = defaultValue;
 else if ([keyValueStr isEqualToString:kNameColorKey])
    nameColorDefault = defaultValue;
  else if ([keyValueStr isEqualToString:kBackgroundColorKey])
   backgroundColorDefault = defaultValue;

  NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
  firstNameDefault, kFirstNameKey,
  lastNameDefault, kLastNameKey,
  nameColorDefault, kNameColorKey,
  backgroundColorDefault, kBackgroundColorKey,
  [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
  [[NSUserDefaults standardUserDefaults] synchronize];

  firstName = [[NSUserDefaults standardUserDefaults]   stringForKey:kFirstNameKey];
  lastName = [[NSUserDefaults standardUserDefaults]   stringForKey:kLastNameKey];
  textColor = [[NSUserDefaults standardUserDefaults]   integerForKey:kNameColorKey];
  backgroundColor = [[NSUserDefaults standardUserDefaults]   integerForKey:kBackgroundColorKey];

Step 7: In the settings.bundle , we’ll see .plist where all the informations are already set what we want to display.

Step 8: In the ApplicationSettingViewController.h file make the following changes.

 @interface ApplicationSettingViewController : UITableViewController {

  UIView *instructionsView;
  UIBarButtonItem *doneButton;
  UIBarButtonItem *flipButton;

@property (nonatomic, retain) IBOutlet UIView *instructionsView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, retain) UIBarButtonItem *flipButton;

- (void)flipAction:(id)sender;

Step 9: Double click the MainWindow.xib file , and open it to the Interface Builder. Drag Navigation Controller from the library and place it to the main window. Open the Navigation controller, drag table view from the library and place it to the navigation window. Now select view controller and bring up Identity Inspector change the class name into the ApplicationSettingViewController. Drag view from the library and place it to the main window change the name into intructionsView. Now drag Bar Button Item from the library and place it on the mainwindow. Connect the Application Setting App Delegate to the Navigation Controller.  Now save the MainWindow.xib file and go back to the Xcode.

Step 10: Now make the following changes in the ApplicationSettingViewController.m file.

 - (void)viewDidLoad
   [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(updateSettings:)
  name:UIApplicationDidFinishLaunchingNotification object:nil];

  UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
  [infoButton addTarget:self action:@selector(flipAction:)   forControlEvents:UIControlEventTouchUpInside];
   flipButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
  self.navigationItem.rightBarButtonItem = flipButton;


 - (void)viewDidUnload
   self.instructionsView = nil;
   self.doneButton = nil;
   self.flipButton = nil;

 - (void)updateSettings:(NSNotification *)notif
   ApplicationSettingAppDelegate *appDelegate = (ApplicationSettingAppDelegate *)[[UIApplication sharedApplication] delegate];
   switch ([appDelegate backgroundColor])
   case blackBackColor:
   self.tableView.backgroundColor = [UIColor blackColor];

  case whiteBackColor:
  self.tableView.backgroundColor = [UIColor whiteColor];

  case blueBackColor:
  self.tableView.backgroundColor = [UIColor blueColor];

   case patternBackColor:
   self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];

#pragma mark - Flip screen
 - (void)flipAction:(id)sender
   [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:animationIDfinished:finished:context:)];
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.75];

   [UIView setAnimationTransition:([self.tableView superview] ?
    UIViewAnimationTransitionFlipFromLeft :    UIViewAnimationTransitionFlipFromRight)
    forView:self.tableView cache:YES];

  if ([instructionsView superview])
  [instructionsView removeFromSuperview];
  [self.tableView addSubview:instructionsView];

  [UIView commitAnimations];

  if ([instructionsView superview] == self.tableView)
  self.navigationItem.rightBarButtonItem = doneButton;
  self.navigationItem.rightBarButtonItem = flipButton;

#pragma mark - UITableViewDataSource

 - (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section
 return 1;

 - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
  NSString *kCellIdentifier = @"MyIdentifier";

  UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:kCellIdentifier];

 if (cell == nil)
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:kCellIdentifier] autorelease];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;

 ApplicationSettingAppDelegate *appDelegate = (ApplicationSettingAppDelegate *)[[UIApplication sharedApplication] delegate];

  NSString *firstNameStr = appDelegate.firstName;
  NSString *lastNameStr = appDelegate.lastName;
  cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", firstNameStr,    lastNameStr];

 switch ([appDelegate textColor])
  case blue:
  cell.textLabel.textColor = [UIColor blueColor];

  case green:
  cell.textLabel.textColor = [UIColor greenColor];

  case red:
  cell.textLabel.textColor = [UIColor redColor];

  return cell;

Step 11: Now compile and run the application in the Simulator.

  • Rick
  • March 5, 2010
  • iOS

About the Author

Leave a Reply 3 comments

Imran Pariyani - March 9, 2011 Reply

Thanks for the tutorial .. is it possible for you to upload the source code for to app somewhere?

Nida - June 1, 2011 Reply

Its very useful tutorial, Please upload the source code as. Thanks

Jaimin Modi - January 2, 2013 Reply

Please upload your source code in xcode 4.2.5 i didn’t get a way to create this settings bundle.

Leave a Reply: