在调整中使用 HBPreferences

Using HBPreferences in a tweak

我们什么时候用这个?它注册了一个变量!我们可以使用 iPhonedevwiki 中给出的古老而简单的方法来做到这一点,并添加一个观察者来观察偏好中所做的更改。但我看到一些调整使用 HBPreferences 而不是 iPhonedevwiki 中给出的方法。有人能告诉我它是如何工作的吗?它比 iPhonedevwiki 中给出的方法更好吗?谢谢。

你可以这样使用

static NSString *const kHBCBPreferencesDomain = @"ws.hbang.cobalia";
static NSString *const kHBCBPreferencesEnabledKey = @"Enabled";
static NSString *const kHBCBPreferencesSwitchesKey = @"Switches";
static NSString *const kHBCBPreferencesSectionLabelKey = @"SectionLabel";
static NSString *const kHBCBPreferencesSwitchLabelsKey = @"SwitchLabels";

HBPreferences *preferences = [[HBPreferences alloc] initWithIdentifier:kHBCBPreferencesDomain];

[preferences registerDefaults:@{
    kHBCBPreferencesEnabledKey: @YES,
    kHBCBPreferencesSwitchesKey: @[ /* ... */ ],
    kHBCBPreferencesSectionLabelKey: @YES,
    kHBCBPreferencesSwitchLabelsKey: @YES
}];

并添加观察者以进行更改

void HBCBPreferencesChanged() {
    //Will be called when changes occur
}

/* Call this function to register an observer */
void addObserver(){
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)HBCBPreferencesChanged, CFSTR("ws.hbang.cobalia/ReloadPrefs"), NULL, kNilOptions);
}

您可以阅读更多相关信息Here or Here