Search code examples
objective-ccocoa-touchinappsettingskitinappsettings

How do I create my own store by subclassing of IASKAbstractSettingsStore?


First off, InAppSettingsKit is just what I was looking for. Once I figure out some things it will save me tons of time.

My question is: how do I create my own store by subclassing IASKAbstractSettingsStore? The home of IASK states:

The default behaviour of IASK is to store the settings in [NSUserDefaults standardUserDefaults]. However, it is possible to change this behaviour by setting the settingsStore property on an IASKAppSettingsViewController.

and

The easiest way to create your own store is to create a subclass of IASKAbstractSettingsStore.

I've spent a good deal of time combing through the code, and I think I understand the basic structure of it. However, I can't figure out how and what to set the settingsStore property to.

I can see the settingsStore defined and implemented in IASKAppSettingsViewController:

id<IASKSettingsStore>  _settingsStore; 

and

- (id<IASKSettingsStore>)settingsStore {
if (!_settingsStore) {
    _settingsStore = [[IASKSettingsStoreUserDefaults alloc] init];
}
return _settingsStore;

}

I tried subclassing IASKAbstractSettingsStore:

#import <Foundation/Foundation.h>
#import "IASKSettingsStore.h"

@interface IASKSettingsStoreMeals : IASKAbstractSettingsStore {
NSString * _filePath;
NSMutableDictionary * _dict;
}
- (id)initWithPath:(NSString*)path;
@end

and then modified IASKAppSettingsViewController's settingsStore property to allocate and initialize my new class IASKSettingsStoreMeals instead of IASKSettingsStoreUserDefaults - the only way I can see to change the property:

- (id<IASKSettingsStore>)settingsStore {
if (!_settingsStore) {
    _settingsStore = [[IASKSettingsStoreMeals alloc] init];
}
return _settingsStore;

}

When I build and run, I get the following message when I try the first control (the toggle switch), all other fields do not get saved:

attempt to insert nil value at objects[0] (key: toggleSwitch)

What am I doing wrong? In addition to the changes needed to "rejigger" the code to use IASKSettingsStoreFile (or a subclassed IASKAbstractSettingsStore), I also can't see where to set the file path change the location of where the settings are saved - or is that done behind the scenes. Looking forward to get past this learning curve and using this.


Solution

  • Found the answer.

    My question reveals my inexperience with object orientated languages on the whole, and the concept of encapsulation and frameworks in particular. No changes needed to be made to the IASK framework code, all code was added on my root view controller.

    I created another instance of IASKAppSettingsViewController, and added the following code to change the plist location:

    // the path to write file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *settingsFile = [documentsDirectory stringByAppendingPathComponent:@"mySettings"];
    IASKSettingsStoreFile *mySettingsBundle = [[IASKSettingsStoreFile alloc] initWithPath:settingsFile]; 
    
    self.appSettingsViewController.settingsStore = mySettingsBundle;
    
    UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:self.appSettingsViewController];
    [mySettingsBundle release];
    
    self.appSettingsViewController.settingsStore = mySettingsBundle;
    
    //[viewController setShowCreditsFooter:NO];   // Uncomment to not display InAppSettingsKit credits for creators.
    // But we encourage you not to uncomment. Thank you!
    self.appSettingsViewController.showDoneButton = YES;
    [self presentModalViewController:aNavController animated:YES];
    [aNavController release];