Preference Center

AirshipPreferenceCenter

AirshipPreferenceCenter is provided as a standalone framework, to access the preference center you must add an import statement in your code, as shown below:

import AirshipPreferenceCenter
@import AirshipPreferenceCenter;

Displaying the Preference Center

The preference center can be displayed with a simple method call on the PreferenceCenter instance. By wiring this method call to a button in your app, you can quickly produce a user-initiated Preference Center with no additional effort.

Displaying the Preference Center
PreferenceCenter.shared.open("preferenceCenterID")
[[UAPreferenceCenter shared] openPreferenceCenter:@"preferenceCenterID"];
}

Adding a Custom Look and Feel

The default implementation displays with default settings for elements such as colors, fonts, and title text. In the following section we’ll explore ways to customize its look and feel without having to create custom UI.

Most developers will want to customize the look and feel to match their app’s existing style and layout. By setting a handful of top-level properties for the Preference Center you can override the default style to quickly arrive at a look and feel matching that of your app. We’ll cover a few of these to show how easy this process can be.

Styling the Preference Center

The preference center’s look can be customized by creating a PreferenceCenterStyle instance, setting its style properties, and then setting the style property of the Preference Center instance to the customized style instance.

For instance, we may want to change the color of the navigation bar in order to match our app and set custom fonts for the title and table view sections and preferences. In the example below, we assume the app has access to the “Roboto” font, but any stock or custom font may be used.

Styling the Preference Center
let style = PreferenceCenterStyle()

// Customize the style object
style.navigationBarColor = UIColor(red: 0.988, green: 0.694, blue: 0.106, alpha: 1)
style.titleColor = UIColor(red: 0.039, green: 0.341, blue: 0.490, alpha: 1)
style.tintColor = UIColor(red: 0.039, green: 0.341, blue: 0.490, alpha: 1)

style.titleFont = UIFont(name: "Roboto-Regular", size: 17.0)
style.sectionTextFont = UIFont(name: "Roboto-Bold", size: 14.0)
style.preferenceTextFont = UIFont(name: "Roboto-Light", size: 12.0)

// Set the style on the default Preference Center UI
PreferenceCenter.shared.style = style
UAPreferenceCenterStyle *style = [[UAPreferenceCenterStyle alloc] init];

// Customize the style object
style.navigationBarColor = [UIColor colorWithRed:0.988 green:0.694 blue:0.106 alpha:1];
style.titleColor = [UIColor colorWithRed:0.039 green:0.341 blue:0.490 alpha:1];
style.tintColor = [UIColor colorWithRed:0.039 green:0.341 blue:0.490 alpha:1];

style.titleFont = [UIFont fontWithName:@"Roboto-Regular" size:17.0];
style.sectionTextFont = [UIFont fontWithName:@"Roboto-Bold" size:13.0];
style.preferenceTextFont = [UIFont fontWithName:@"Roboto-Light" size:12.0];

// Set the style on the default Preference Center UI
[UAPreferenceCenter shared].style = style;

This can also be done without writing code by creating a plist file. Create a plist with the desired preference center style. All the keys correspond to properties on the PreferenceCenterStyle class.

Colors are represented by strings, either a valid color hexadecimal or a named color. Named color strings must correspond to a named color defined in a color asset within the main bundle.

Save the plist as AirshipPreferenceCenterStyle.plist file.

Styling the Preference Center using a plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>titleFont</key>
        <dict>
                <key>fontName</key>
                <string>Roboto-Regular</string>
                <key>fontSize</key>
                <string>17</string>
        </dict>
        <key>titleColor</key>
        <string>#00698f</string>
        <key>tintColor</key>
        <string>#00698f</string>
        <key>navigationBarColor</key>
        <string>myNavBarColor</string>
        <key>sectionTextFont</key>
        <dict>
                <key>fontName</key>
                <string>Roboto-Bold</string>
                <key>fontSize</key>
                <string>13</string>
        </dict>
        <key>preferenceTextFont</key>
        <dict>
                <key>fontName</key>
                <string>Roboto-Light</string>
                <key>fontSize</key>
                <string>17</string>
        </dict>
</dict>
</plist>

Overriding the Preference Center

For custom Preference Center implementations, initialize the PreferenceCenterOpenDelegate with the custom Preference Center implementation.

Overriding the Preference Center
PreferenceCenter.shared.openDelegate = self

func openPreferenceCenter(_ preferenceCenterID: String) -> Bool {
    /// implement custom preference center
    return true
}
[UAPreferenceCenter shared].openDelegate = self;
    
- (BOOL)openPreferenceCenter:(NSString * _Nonnull)preferenceCenterID { 
    // implement custom preference center
    return YES;
}

To retrieve the Preference Center config, you can use the following method:

PreferenceCenter.shared.config(preferenceCenterID: "preferenceCenterID") { config in
    /// retrieve config
}
[[UAPreferenceCenter shared] configForPreferenceCenterID:@"preferenceCenterID" completionHandler:^(UAPreferenceCenterConfig *config) {
    // retrieve config    
}];