Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add check for attribution button hiding and telemetry opt-out
Browse files Browse the repository at this point in the history
  • Loading branch information
friedbunny committed Jan 8, 2016
1 parent 075c30f commit 09ab2ed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ IB_DESIGNABLE
[required](https://www.mapbox.com/help/metrics-opt-out-for-users/) to
provide users with the option to disable anonymous usage and location
sharing (telemetry). If this view is hidden, you must implement this
setting elsewhere in your app or via `Settings.bundle`.
setting elsewhere in your app or via `Settings.bundle`. See our
[website](https://www.mapbox.com/ios-sdk/#telemetry_opt_out)
for implementation help.
*/
@property (nonatomic, readonly) UIButton *attributionButton;

Expand Down
14 changes: 13 additions & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ - (void)commonInit
_attributionButton.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_attributionButton];
_attributionButtonConstraints = [NSMutableArray array];
[_attributionButton addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:NULL];

// setup compass
//
Expand Down Expand Up @@ -481,6 +482,7 @@ - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];
[_attributionButton removeObserver:self forKeyPath:@"hidden"];

[self validateDisplayLink];

Expand Down Expand Up @@ -1486,7 +1488,8 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
}
else if (buttonIndex == alertView.firstOtherButtonIndex)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://mapbox.com/telemetry/"]];
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:@"https://mapbox.com/telemetry/"]];
}
else if (buttonIndex == alertView.firstOtherButtonIndex + 1)
{
Expand All @@ -1506,6 +1509,15 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
_mbglFileSource->setAccessToken((std::string)[accessToken UTF8String]);
}
}
else if ([keyPath isEqualToString:@"hidden"] && object == _attributionButton)
{
NSNumber *hiddenNumber = change[NSKeyValueChangeNewKey];
BOOL attributionButtonWasHidden = [hiddenNumber boolValue];
if (attributionButtonWasHidden)
{
[MGLMapboxEvents ensureMetricsOptoutExists];
}
}
}

+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingZoomEnabled
Expand Down
5 changes: 4 additions & 1 deletion platform/ios/src/MGLMapboxEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ typedef NS_MUTABLE_DICTIONARY_OF(NSString *, id) MGLMutableMapboxEventAttributes
+ (void) flush;

// Main thread only
+ (void)validate;
+ (void) validate;

// Main thread only
+ (void) ensureMetricsOptoutExists;

@end

Expand Down
34 changes: 34 additions & 0 deletions platform/ios/src/MGLMapboxEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,40 @@ + (BOOL) checkPushEnabled {
return result;
}

// Main thread only
//
+ (void) ensureMetricsOptoutExists {
NSNumber *shownInAppNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"];
BOOL hasMetricsEnabledSettingShownInAppFlag = [shownInAppNumber boolValue];

if ( ! hasMetricsEnabledSettingShownInAppFlag &&
[[NSUserDefaults standardUserDefaults] integerForKey:@"MGLMapboxAccountType"] == 0) {
// Opt-out is not configured in UI, so check for Settings.bundle
id defaultEnabledValue;
NSString *appSettingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

if (appSettingsBundle) {
// Dynamic Settings.bundle loading based on http://stackoverflow.com/a/510329/2094275
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"];
}
}
}

if ( ! defaultEnabledValue) {
[NSException raise:@"Telemetry opt-out missing" format:
@"End users must be able to opt out of Mapbox Telemetry in your app, either inside Settings (via Settings.bundle) or inside this app. "
@"By default, this opt-out control is included as a menu item in the attribution action sheet. "
@"If you reimplement the opt-out control inside this app, disable this assertion by setting MGLMapboxMetricsEnabledSettingShownInApp to YES in Info.plist."
@"\r\n\r\nSee https://www.mapbox.com/ios-sdk/#telemetry_opt_out for more information."
@"\r\n\r\nAdditionally, by hiding this attribution control you agree to display the required attribution elsewhere in this app."];
}
}
}

#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// Iterate through locations to pass all data
Expand Down

1 comment on commit 09ab2ed

@friedbunny
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1ec5 How does this look?

Please sign in to comment.