Search code examples
iosobjective-cjailbreaktheos

Using HBPreferences in a tweak


When do we use this? It registers a variable! We can do this using the old and simple way as given in iPhonedevwiki and add an observer to observe the changes made in preferences. But I see some tweaks use HBPreferences instead of the method given in iPhonedevwiki. Could someone please tell me how it works and is it better than the methods given in iPhonedevwiki? Thanks.


Solution

  • You can use it like this

    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
    }];
    

    And to add an observer for taking changes

    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);
    }
    

    You can read more about it Here or Here