diff options
author | Jason Wray <jason@mapbox.com> | 2019-08-20 16:11:11 -0700 |
---|---|---|
committer | Jason Wray <friedbunny@users.noreply.github.com> | 2019-08-21 12:14:20 -0700 |
commit | 0e57c7de4401ed67854b64b1bbc26edf04e2e07c (patch) | |
tree | 78e56c481631946d1a1c22a129a887ed02ef1f1c | |
parent | c3cd9002f0f59ff6955c75410aaae62fb782935d (diff) | |
download | qtlocation-mapboxgl-0e57c7de4401ed67854b64b1bbc26edf04e2e07c.tar.gz |
[ios] Refactor NSUserDefaults key variable names
-rw-r--r-- | platform/darwin/src/MGLAccountManager.m | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLAccountManager_Private.h | 3 | ||||
-rw-r--r-- | platform/darwin/src/http_file_source.mm | 9 | ||||
-rw-r--r-- | platform/ios/src/MGLMapView.mm | 6 | ||||
-rw-r--r-- | platform/ios/src/MGLMapboxEvents.h | 3 | ||||
-rw-r--r-- | platform/ios/src/MGLMapboxEvents.m | 66 |
6 files changed, 49 insertions, 40 deletions
diff --git a/platform/darwin/src/MGLAccountManager.m b/platform/darwin/src/MGLAccountManager.m index 69170459bc..c37195967a 100644 --- a/platform/darwin/src/MGLAccountManager.m +++ b/platform/darwin/src/MGLAccountManager.m @@ -13,6 +13,8 @@ static NSString * const MGLAccountManagerExternalClassName = @"MBXAccounts"; static NSString * const MGLAccountManagerExternalMethodName = @"skuToken"; #endif +NSString * const MGLMapboxAccountTypeKey = @"MGLMapboxAccountType"; + @interface MGLAccountManager () @property (atomic) NSString *accessToken; diff --git a/platform/darwin/src/MGLAccountManager_Private.h b/platform/darwin/src/MGLAccountManager_Private.h index 3fd45f1ae8..4bf7963182 100644 --- a/platform/darwin/src/MGLAccountManager_Private.h +++ b/platform/darwin/src/MGLAccountManager_Private.h @@ -2,6 +2,9 @@ NS_ASSUME_NONNULL_BEGIN +/// NSUserDefaults key that controls developer account type +FOUNDATION_EXTERN NSString * const MGLMapboxAccountTypeKey; + @interface MGLAccountManager (Private) /// Returns the shared instance of the `MGLAccountManager` class. diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index c2ed065ed8..b0f69f4a7f 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -88,15 +88,14 @@ class HTTPFileSource::Impl { public: Impl() { @autoreleasepool { - - NSURLSessionConfiguration *sessionConfig = - [MGLNetworkConfiguration sharedManager].sessionConfiguration; - + NSURLSessionConfiguration *sessionConfig = [MGLNetworkConfiguration sharedManager].sessionConfiguration; session = [NSURLSession sessionWithConfiguration:sessionConfig]; userAgent = getUserAgent(); - accountType = [[NSUserDefaults standardUserDefaults] integerForKey:@"MGLMapboxAccountType"]; +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + accountType = [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountTypeKey]; +#endif } } diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 51102233cf..d242feb38d 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -2425,7 +2425,7 @@ public: NSString *message; NSString *participateTitle; NSString *declineTitle; - if ([[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsEnabled"]) + if ([[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsEnabledKey]) { message = NSLocalizedStringWithDefaultValue(@"TELEMETRY_ENABLED_MSG", nil, nil, @"You are helping to make OpenStreetMap and Mapbox maps better by contributing anonymous usage data.", @"Telemetry prompt message"); participateTitle = NSLocalizedStringWithDefaultValue(@"TELEMETRY_ENABLED_ON", nil, nil, @"Keep Participating", @"Telemetry prompt button"); @@ -2453,14 +2453,14 @@ public: UIAlertAction *declineAction = [UIAlertAction actionWithTitle:declineTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"MGLMapboxMetricsEnabled"]; + [[NSUserDefaults standardUserDefaults] setBool:NO forKey:MGLMapboxMetricsEnabledKey]; }]; [alertController addAction:declineAction]; UIAlertAction *participateAction = [UIAlertAction actionWithTitle:participateTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MGLMapboxMetricsEnabled"]; + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:MGLMapboxMetricsEnabledKey]; }]; [alertController addAction:participateAction]; diff --git a/platform/ios/src/MGLMapboxEvents.h b/platform/ios/src/MGLMapboxEvents.h index cb3132656f..a7d316cc06 100644 --- a/platform/ios/src/MGLMapboxEvents.h +++ b/platform/ios/src/MGLMapboxEvents.h @@ -3,6 +3,9 @@ NS_ASSUME_NONNULL_BEGIN +/// NSUserDefaults key that controls telemetry user opt-out status +FOUNDATION_EXTERN NSString * const MGLMapboxMetricsEnabledKey; + @interface MGLMapboxEvents : NSObject + (nullable instancetype)sharedInstance; diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m index cc7390ac61..808c3a88bf 100644 --- a/platform/ios/src/MGLMapboxEvents.m +++ b/platform/ios/src/MGLMapboxEvents.m @@ -3,14 +3,16 @@ #import "NSBundle+MGLAdditions.h" #import "MGLAccountManager_Private.h" +// NSUserDefaults and Info.plist keys +NSString * const MGLMapboxMetricsEnabledKey = @"MGLMapboxMetricsEnabled"; +static NSString * const MGLMapboxMetricsDebugLoggingEnabledKey = @"MGLMapboxMetricsDebugLoggingEnabled"; +static NSString * const MGLMapboxMetricsEnabledSettingShownInAppKey = @"MGLMapboxMetricsEnabledSettingShownInApp"; +static NSString * const MGLTelemetryAccessTokenKey = @"MGLTelemetryAccessToken"; +static NSString * const MGLTelemetryBaseURLKey = @"MGLTelemetryBaseURL"; +static NSString * const MGLEventsProfileKey = @"MMEEventsProfile"; +static NSString * const MGLVariableGeofenceKey = @"VariableGeofence"; + static NSString * const MGLAPIClientUserAgentBase = @"mapbox-maps-ios"; -static NSString * const MGLMapboxAccountType = @"MGLMapboxAccountType"; -static NSString * const MGLMapboxMetricsEnabled = @"MGLMapboxMetricsEnabled"; -static NSString * const MGLMapboxMetricsDebugLoggingEnabled = @"MGLMapboxMetricsDebugLoggingEnabled"; -static NSString * const MGLTelemetryAccessToken = @"MGLTelemetryAccessToken"; -static NSString * const MGLTelemetryBaseURL = @"MGLTelemetryBaseURL"; -static NSString * const MGLEventsProfile = @"MMEEventsProfile"; -static NSString * const MGLVariableGeofence = @"VariableGeofence"; @interface MGLMapboxEvents () @@ -19,16 +21,16 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; @property (nonatomic, copy) NSString *accessToken; @end -@implementation MGLMapboxEvents +@implementation MGLMapboxEvents + (void)initialize { if (self == [MGLMapboxEvents class]) { NSBundle *bundle = [NSBundle mainBundle]; - NSNumber *accountTypeNumber = [bundle objectForInfoDictionaryKey:MGLMapboxAccountType]; - [[NSUserDefaults standardUserDefaults] registerDefaults:@{MGLMapboxAccountType: accountTypeNumber ?: @0, - MGLMapboxMetricsEnabled: @YES, - MGLMapboxMetricsDebugLoggingEnabled: @NO}]; + NSNumber *accountTypeNumber = [bundle objectForInfoDictionaryKey:MGLMapboxAccountTypeKey]; + [[NSUserDefaults standardUserDefaults] registerDefaults:@{MGLMapboxAccountTypeKey: accountTypeNumber ?: @0, + MGLMapboxMetricsEnabledKey: @YES, + MGLMapboxMetricsDebugLoggingEnabledKey: @NO}]; } } @@ -46,9 +48,9 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; self = [super init]; if (self) { _eventsManager = MMEEventsManager.sharedManager; - _eventsManager.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsDebugLoggingEnabled]; - _eventsManager.accountType = [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountType]; - _eventsManager.metricsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsEnabled]; + _eventsManager.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsDebugLoggingEnabledKey]; + _eventsManager.accountType = [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountTypeKey]; + _eventsManager.metricsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsEnabledKey]; // It is possible for the shared instance of this class to be created because of a call to // +[MGLAccountManager load] early on in the app lifecycle of the host application. @@ -57,11 +59,11 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; // (once -[MMEEventsManager initializeWithAccessToken:userAgentBase:hostSDKVersion:] is called. // Normally, the telem access token and base URL are not set this way. However, overriding these values // with user defaults can be useful for testing with an alternative (test) backend system. - if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryAccessToken]) { - self.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryAccessToken]; + if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryAccessTokenKey]) { + self.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryAccessTokenKey]; } - if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryBaseURL]) { - self.baseURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryBaseURL]]; + if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryBaseURLKey]) { + self.baseURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryBaseURLKey]]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil]; @@ -81,22 +83,22 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; } - (void)updateNonDisablingConfigurationValues { - self.eventsManager.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"]; + self.eventsManager.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsDebugLoggingEnabledKey]; - // It is possible for `MGLTelemetryAccessToken` to have been set yet `userDefaultsDidChange:` + // It is possible for the telemetry access token key to have been set yet `userDefaultsDidChange:` // is called before `setupWithAccessToken:` is called. // In that case, setting the access token here will have no effect. In practice, that's fine // because the access token value will be resolved when `setupWithAccessToken:` is called eventually - if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryAccessToken]) { - self.eventsManager.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryAccessToken]; + if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryAccessTokenKey]) { + self.eventsManager.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryAccessTokenKey]; } - // It is possible for `MGLTelemetryBaseURL` to have been set yet `userDefaultsDidChange:` + // It is possible for the telemetry base URL key to have been set yet `userDefaultsDidChange:` // is called before setupWithAccessToken: is called. // In that case, setting the base URL here will have no effect. In practice, that's fine // because the base URL value will be resolved when `setupWithAccessToken:` is called eventually - if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryBaseURL]) { - NSURL *baseURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryBaseURL]]; + if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:MGLTelemetryBaseURLKey]) { + NSURL *baseURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:MGLTelemetryBaseURLKey]]; self.eventsManager.baseURL = baseURL; } } @@ -109,8 +111,8 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; if ([[notification object] respondsToSelector:@selector(objectForKey:)]) { NSUserDefaults *userDefaults = [notification object]; - NSInteger accountType = [userDefaults integerForKey:MGLMapboxAccountType]; - BOOL metricsEnabled = [userDefaults boolForKey:MGLMapboxMetricsEnabled]; + NSInteger accountType = [userDefaults integerForKey:MGLMapboxAccountTypeKey]; + BOOL metricsEnabled = [userDefaults boolForKey:MGLMapboxMetricsEnabledKey]; if (accountType != self.eventsManager.accountType || metricsEnabled != self.eventsManager.metricsEnabled) { self.eventsManager.accountType = accountType; @@ -124,7 +126,7 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; + (void)setupWithAccessToken:(NSString *)accessToken { int64_t delayTime = 0; - if ([[[NSBundle mainBundle] objectForInfoDictionaryKey:MGLEventsProfile] isEqualToString:MGLVariableGeofence]) { + if ([[[NSBundle mainBundle] objectForInfoDictionaryKey:MGLEventsProfileKey] isEqualToString:MGLVariableGeofenceKey]) { delayTime = 10; } @@ -164,11 +166,11 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; } + (void)ensureMetricsOptoutExists { - NSNumber *shownInAppNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"]; + NSNumber *shownInAppNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:MGLMapboxMetricsEnabledSettingShownInAppKey]; BOOL metricsEnabledSettingShownInAppFlag = [shownInAppNumber boolValue]; if (!metricsEnabledSettingShownInAppFlag && - [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountType] == 0) { + [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountTypeKey] == 0) { // Opt-out is not configured in UI, so check for Settings.bundle id defaultEnabledValue; NSString *appSettingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"]; @@ -178,7 +180,7 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence"; NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[appSettingsBundle stringByAppendingPathComponent:@"Root.plist"]]; NSArray *preferences = settings[@"PreferenceSpecifiers"]; for (NSDictionary *prefSpecification in preferences) { - if ([prefSpecification[@"Key"] isEqualToString:MGLMapboxMetricsEnabled]) { + if ([prefSpecification[@"Key"] isEqualToString:MGLMapboxMetricsEnabledKey]) { defaultEnabledValue = prefSpecification[@"DefaultValue"]; } } |