Enable Metrics by default default default

Enable Mapbox Metrics by default when Settings.bundle is absent, but allow Settings.bundle to override the default. Don’t register defaults for non-Mapbox preferences. The two-phase registration ensures that the defaults don’t default to unexpected values, yet gives the developer a chance to call `+[MGLAccountManager mapboxMetricsEnabledSettingShownInApp]` before the hard assertion.

Fixes #1518.
This commit is contained in:
Minh Nguyễn
2015-05-12 15:26:07 -07:00
parent 50d7add6b0
commit afbb9c2717

View File

@@ -110,6 +110,15 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
@implementation MGLMapboxEvents
+ (void)initialize {
if (self == [MGLMapboxEvents class]) {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"MGLMapboxAccountType": @0,
@"MGLMapboxMetricsEnabled": @YES,
}];
}
}
// Must be called from the main thread. Only called internally.
//
- (instancetype) init {
@@ -121,23 +130,25 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
if (! [MGLAccountManager mapboxMetricsEnabledSettingShownInApp]) {
// Opt Out is not configured in UI, so check for Settings.bundle
// Put Settings bundle into memory
id defaultEnabledValue;
NSString *appSettingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
NSAssert(appSettingsBundle, @"End users must be able to opt out of Metrics in your app, either inside Settings (via Settings.bundle) or inside this app. If you implement the opt-out control inside this app, disable this assertion by setting [MGLAccountManager setMapboxMetricsEnabledSettingShownInApp:YES] before the app initializes any Mapbox GL classes.");
// Dynamic Settings.bundle loading based on:
// http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[appSettingsBundle stringByAppendingPathComponent:@"Root.plist"]];
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences) {
NSString *key = [prefSpecification objectForKey:@"Key"];
if(key && [[prefSpecification allKeys] containsObject:@"DefaultValue"]) {
[defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
if (appSettingsBundle) {
// Dynamic Settings.bundle loading based on:
// http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[appSettingsBundle stringByAppendingPathComponent:@"Root.plist"]];
NSArray *preferences = settings[@"PreferenceSpecifiers"];
for (NSDictionary *prefSpecification in preferences) {
if ([prefSpecification[@"Key"] isEqualToString:@"MGLMapboxMetricsEnabled"]) {
defaultEnabledValue = prefSpecification[@"DefaultValue"];
}
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
NSAssert(defaultEnabledValue, @"End users must be able to opt out of Metrics in your app, either inside Settings (via Settings.bundle) or inside this app. If you implement the opt-out control inside this app, disable this assertion by setting [MGLAccountManager setMapboxMetricsEnabledSettingShownInApp:YES] before the app initializes any Mapbox GL classes.");
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"MGLMapboxMetricsEnabled": defaultEnabledValue,
}];
}
_appBundleId = [[NSBundle mainBundle] bundleIdentifier];