iOS用户首选项保存和更新

时间:2015-05-06 15:22:42

标签: ios objective-c

我认为这个问题很难被问到,所以请耐心等待,如果没有意义,请告诉我。

我将从解释情况开始,然后展示一些代码。所以我有一个应用程序,它有一组“默认”设置显示在用户的仪表板上。用户可以修改这些设置以启用或禁用其仪表板上显示的某些项目。

我希望能够更新“默认”设置,如果我添加新项目,它也会自动更新用户的设置,但不会覆盖其个人启用/禁用设置。

所以我有这个词典数组,“默认”:

dashboardItems = @[
                   @{
                       @"id" : @"1",
                       @"order" : @"0",
                       @"title" : @"Steps Traveled",
                       @"unit" : @"",
                       @"type" : HKQuantityTypeIdentifierStepCount,
                       @"sampleUnit" : @"count",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"2",
                       @"order" : @"1",
                       @"title" : @"Flights Climbed",
                       @"unit" : @"",
                       @"type" : HKQuantityTypeIdentifierFlightsClimbed,
                       @"sampleUnit" : @"count",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"3",
                       @"order" : @"2",
                       @"title" : @"Distance Traveled",
                       @"unit" : @"mi",
                       @"type" : HKQuantityTypeIdentifierDistanceWalkingRunning,
                       @"sampleUnit" : @"mi",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"4",
                       @"order" : @"3",
                       @"title" : @"Active Calories",
                       @"unit" : @"",
                       @"type" : HKQuantityTypeIdentifierActiveEnergyBurned,
                       @"sampleUnit" : @"kcal",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"5",
                       @"order" : @"4",
                       @"title" : @"Weight",
                       @"unit" : @"lbs",
                       @"type" : HKQuantityTypeIdentifierBodyMass,
                       @"sampleUnit" : @"lb",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"6",
                       @"order" : @"5",
                       @"title" : @"Cycling",
                       @"unit" : @"mi",
                       @"type" : HKQuantityTypeIdentifierDistanceCycling,
                       @"sampleUnit" : @"mi",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"7",
                       @"order" : @"6",
                       @"title" : @"Heart Rate",
                       @"unit" : @"BPM",
                       @"type" : HKQuantityTypeIdentifierHeartRate,
                       @"sampleUnit" : @"count/min",
                       @"enabled" : @"1"
                   },
                   @{
                       @"id" : @"8",
                       @"order" : @"7",
                       @"title" : @"BMI",
                       @"unit" : @"",
                       @"type" : HKQuantityTypeIdentifierBodyMassIndex,
                       @"sampleUnit" : @"count",
                       @"enabled" : @"1"
                   }
        ];

因此,到目前为止,用户可以使用开关将启用从1更改为0,反之亦然以打开或关闭项目?

当用户注册应用时,将“默认”设置保存到NSUserDefaults,并保存到实时服务器以备用,以防用户删除应用。

如果用户注销或删除应用,我们会在下次登录时检查他们当前是否已保存NSUserDefaults,如果不是,我们会从服务器中提取。

然后我比较2个数组以查看是否有任何项目被更改或添加到“默认值”,这是最好的方法吗?

//here we need to check if the user does not have any preferences saved, if not pull them from Parse.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *dashboardItems = [defaults objectForKey:@"dashboardItems"];

if (dashboardItems == nil) {

    //user has no preferences so load them in
    dashboardItems = [NSJSONSerialization JSONObjectWithData:[user[@"preferences"] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

    [defaults setObject:dashboardItems forKey:@"dashboardItems"];
    [defaults synchronize];

}

//compare current preferences against default to see if any new items were changed
NSArray *defaultPreferences = [[PreferencesManager sharedManager] dashboardItems];

for (NSDictionary *item in defaultPreferences) {

    for (NSDictionary *userItem in dashboardItems) {

        if ([item[@"id"] isEqualToString:userItem[@"id"]]) {

            //we have a match, compare name and change if necessary
            if (![item[@"title"] isEqualToString:userItem[@"title"]]) {

                //set user's item title to default title
                [userItem setValue:item[@"title"] forKey:@"title"];

            }

        } else {

            //we did not find this in user preferences, so add it
            [dashboardItems addObject:item];

        }

    }


}

//save defaults
[defaults setObject:dashboardItems forKey:@"dashboardItems"];
[defaults synchronize];

如果我需要进一步澄清,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

我首先获取所有默认设置,然后替换存储用户设置中匹配的设置:

NSMutableArray *defaultSettings = [@[] mutableCopy];
NSArray *userSettings = @[];
for (NSInteger i = 0; i < defaultSettings.count; i++) {
    NSDictionary *setting = defaultSettings[i];
    NSInteger index = [userSettings indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        NSDictionary *userSetting = obj;
        *stop = [setting[@"title"] isEqualToString:userSetting[@"title"]];
        return *stop;
    }];
    if (index != NSNotFound) {
        // Found matching setting
        defaultSettings[i] = userSettings[index];
    }
}

[defaults setObject:defaultSettings forKey:@"defaultSettings"];
[defaults synchronize];

但如果我是你,我会使用NSDictionary的NSDictionary,并使用密钥作为ID。这将使同样的过程变得更加容易:

NSDictionary *defaultSettings = @{
                                  @"1": @{@"order": @"0", @"title": @"Steps traveled", @"enabled": @"0"},
                                  @"2": @{@"order": @"1", @"title": @"Flights Climbed", @"enabled": @"0"},
                                  @"3": @{@"order": @"2", @"title": @"Distance Traveled", @"enabled": @"0"}
                                  };
NSMutableDictionary *mutableDefaultSettings = [defaultSettings mutableCopy];
NSDictionary *userSettings = @{
                               @"2": @{@"order": @"1", @"title": @"Flights Climbed", @"enabled": @"1"}
                               };

for (NSString *id in userSettings.allKeys) {
    mutableDefaultSettings[id] = userSettings[id];
}

[defaults setObject:mutableDefaultSettings forKey:@"dashboardItems"];
[defaults synchronize];