summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gyp/platform-ios.gypi1
-rw-r--r--include/mbgl/ios/MGLAccountManager.h2
-rw-r--r--include/mbgl/ios/MGLMapView.h27
-rw-r--r--ios/app/MBXAppDelegate.m32
-rw-r--r--platform/ios/MGLAccountManager.m17
-rw-r--r--platform/ios/MGLAccountManager_Private.h10
-rw-r--r--platform/ios/MGLMapView.mm42
-rw-r--r--platform/ios/MGLMapboxEvents.m6
8 files changed, 83 insertions, 54 deletions
diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi
index 0034831a91..0ec67786a7 100644
--- a/gyp/platform-ios.gypi
+++ b/gyp/platform-ios.gypi
@@ -26,6 +26,7 @@
'../platform/ios/MGLFileCache.h',
'../platform/ios/MGLFileCache.mm',
'../include/mbgl/ios/MGLAccountManager.h',
+ '../platform/ios/MGLAccountManager_Private.h',
'../platform/ios/MGLAccountManager.m',
'../include/mbgl/ios/MGLAnnotation.h',
'../include/mbgl/ios/MGLUserLocation.h',
diff --git a/include/mbgl/ios/MGLAccountManager.h b/include/mbgl/ios/MGLAccountManager.h
index 7ec4135f18..6b127543ee 100644
--- a/include/mbgl/ios/MGLAccountManager.h
+++ b/include/mbgl/ios/MGLAccountManager.h
@@ -21,7 +21,7 @@
/** Certain Mapbox plans require the collection of user metrics. If you aren't using a preference switch in an existing or new `Settings.bundle` in your application, set this value to `YES` to indicate that you are providing a metrics opt-out for users within your app's interface directly.
* @param showsOptOut Whether your application's interface provides a user opt-out preference. The default value is `NO`, meaning a `Settings.bundle` is expected for providing a user opt-out preference. */
-+ (void)setMapboxMetricsEnabledSettingShownInApp:(BOOL)showsOptOut;
++ (void)setMapboxMetricsEnabledSettingShownInApp:(BOOL)showsOptOut __attribute__((unavailable("Set MGLMapboxMetricsEnabledSettingShownInApp in Info.plist.")));
/** Whether in-app user metrics opt-out is configured. If set to the default value of `NO`, a user opt-out preference is expected in a `Settings.bundle` that shows in the application's section within the system Settings app. */
+ (BOOL)mapboxMetricsEnabledSettingShownInApp;
diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h
index 9d7b5c9019..2d6589bf0d 100644
--- a/include/mbgl/ios/MGLMapView.h
+++ b/include/mbgl/ios/MGLMapView.h
@@ -20,30 +20,25 @@ IB_DESIGNABLE
/** @name Initializing a Map View */
-/** Initialize a map view with the default style, given frame, and access token set in MapboxGL singleton.
-* @param frame The frame with which to initialize the map view.
-* @return An initialized map view, or `nil` if the map view was unable to be initialized. */
+/** Initializes and returns a newly allocated map view with the specified frame and the default style.
+* @param frame The frame for the view, measured in points.
+* @return An initialized map view or `nil` if the map view couldn’t be created. */
- (instancetype)initWithFrame:(CGRect)frame;
+- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken __attribute__((unavailable("Use -initWithFrame:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:].")));
-/** Initialize a map view with the default style and a given frame and access token.
-* @param frame The frame with which to initialize the map view.
-* @param accessToken A Mapbox API access token.
-* @return An initialized map view, or `nil` if the map view was unable to be initialized. */
-- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken;
-
-/** Initialize a map view with a given frame, access token, and style URL.
- * @param frame The frame with which to initialize the map view.
- * @param accessToken A Mapbox API access token.
- * @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://<user.style>`).
- * @return An initialized map view, or `nil` if the map view was unable to be initialized. */
-- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL;
+/** Initializes and returns a newly allocated map view with the specified frame and style URL.
+* @param frame The frame for the view, measured in points.
+* @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://<user.style>`).
+* @return An initialized map view or `nil` if the map view couldn’t be created. */
+- (instancetype)initWithFrame:(CGRect)frame styleURL:(NSURL *)styleURL;
+- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL __attribute__((unavailable("Use -initWithFrame:styleURL:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:].")));
#pragma mark - Authorizing Access
/** @name Authorizing Access */
/** Mapbox API access token for the map view. */
-@property (nonatomic) NSString *accessToken;
+@property (nonatomic) NSString *accessToken __attribute__((unavailable("Use +[MGLAccountManager accessToken] and +[MGLAccountManager setAccessToken:].")));
#pragma mark - Managing Constraints
diff --git a/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m
index dc04105083..b09235a278 100644
--- a/ios/app/MBXAppDelegate.m
+++ b/ios/app/MBXAppDelegate.m
@@ -6,24 +6,22 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
- // Set Access Token
- NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
- if (accessToken) {
- // Store to preferences so that we can launch the app later on without having to specify
- // token.
- [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"];
- } else {
- // Try to retrieve from preferences, maybe we've stored them there previously and can reuse
- // the token.
- accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
- }
- if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work.");
-
- // Signal To SDK That Opt Out Is In App UI
-// [MGLAccountManager setMapboxMetricsEnabledSettingShownInApp:YES];
+ // Set access token, unless MGLAccountManager already read it in from Info.plist.
+ if ( ! [MGLAccountManager accessToken]) {
+ NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
+ if (accessToken) {
+ // Store to preferences so that we can launch the app later on without having to specify
+ // token.
+ [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"];
+ } else {
+ // Try to retrieve from preferences, maybe we've stored them there previously and can reuse
+ // the token.
+ accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
+ }
+ if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work.");
- // Start Mapbox GL SDK
- [MGLAccountManager setAccessToken:accessToken];
+ [MGLAccountManager setAccessToken:accessToken];
+ }
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[MBXViewController new]];
diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m
index a49433777d..2f246b7d91 100644
--- a/platform/ios/MGLAccountManager.m
+++ b/platform/ios/MGLAccountManager.m
@@ -1,6 +1,6 @@
#import <Foundation/Foundation.h>
-#import "MGLAccountManager.h"
+#import "MGLAccountManager_Private.h"
#import "MGLMapboxEvents.h"
#import "NSProcessInfo+MGLAdditions.h"
@@ -14,6 +14,16 @@
@implementation MGLAccountManager
++ (void)load {
+ // Read initial configuration from Info.plist.
+ NSBundle *bundle = [NSBundle bundleForClass:self];
+ self.accessToken = [bundle objectForInfoDictionaryKey:@"MGLMapboxAccessToken"];
+ NSNumber *shownInAppNumber = [bundle objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"];
+ if (shownInAppNumber) {
+ [MGLAccountManager sharedManager].mapboxMetricsEnabledSettingShownInApp = [shownInAppNumber boolValue];
+ }
+}
+
// Can be called from any thread.
//
+ (instancetype) sharedManager {
@@ -25,7 +35,6 @@
void (^setupBlock)() = ^{
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
- _sharedManager.mapboxMetricsEnabledSettingShownInApp = NO;
});
};
if ( ! [[NSThread currentThread] isMainThread]) {
@@ -48,7 +57,9 @@
}
+ (void) setAccessToken:(NSString *) accessToken {
- [[MGLAccountManager sharedManager] setAccessToken:accessToken];
+ if ( ! [accessToken length]) return;
+
+ [MGLAccountManager sharedManager].accessToken = accessToken;
// Update MGLMapboxEvents
// NOTE: This is (likely) the initial setup of MGLMapboxEvents
diff --git a/platform/ios/MGLAccountManager_Private.h b/platform/ios/MGLAccountManager_Private.h
new file mode 100644
index 0000000000..3cc05b09f0
--- /dev/null
+++ b/platform/ios/MGLAccountManager_Private.h
@@ -0,0 +1,10 @@
+#import "MGLAccountManager.h"
+
+@interface MGLAccountManager (Private)
+
+/** Returns the shared instance of the `MGLAccountManager` class. */
++ (instancetype)sharedManager;
+
+@property (atomic) NSString *accessToken;
+
+@end
diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm
index 5d14107518..215be14b54 100644
--- a/platform/ios/MGLMapView.mm
+++ b/platform/ios/MGLMapView.mm
@@ -24,6 +24,7 @@
#import "MGLUserLocationAnnotationView.h"
#import "MGLUserLocation_Private.h"
#import "MGLFileCache.h"
+#import "MGLAccountManager_Private.h"
#import "MGLMapboxEvents.h"
#import "SMCalloutView.h"
@@ -108,25 +109,18 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
if (self && [self commonInit])
{
self.styleURL = nil;
- self.accessToken = [MGLAccountManager accessToken];
return self;
}
return nil;
}
-- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken
-{
- return [self initWithFrame:frame accessToken:accessToken styleURL:nil];
-}
-
-- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL
+- (instancetype)initWithFrame:(CGRect)frame styleURL:(NSURL *)styleURL
{
self = [super initWithFrame:frame];
if (self && [self commonInit])
{
- self.accessToken = accessToken;
self.styleURL = styleURL;
}
@@ -148,18 +142,18 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
- (NSString *)accessToken
{
- return @(_mbglMap->getAccessToken().c_str()).mgl_stringOrNilIfEmpty;
+ NSAssert(NO, @"-[MGLMapView accessToken] has been removed. Use +[MGLAccountManager accessToken] or get MGLMapboxAccessToken from the Info.plist.");
+ return nil;
}
- (void)setAccessToken:(NSString *)accessToken
{
- _mbglMap->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
- [MGLAccountManager setAccessToken:accessToken.mgl_stringOrNilIfEmpty];
+ NSAssert(NO, @"-[MGLMapView setAccessToken:] has been replaced by +[MGLAccountManager setAccessToken:].\n\nIf you previously set this access token in a storyboard inspectable, select the MGLMapView in Interface Builder and delete the “accessToken” entry from the User Defined Runtime Attributes section of the Identity inspector. Then go to the Info.plist file and set MGLMapboxAccessToken to “%@”.", accessToken);
}
+ (NSSet *)keyPathsForValuesAffectingStyleURL
{
- return [NSSet setWithObjects:@"mapID", @"accessToken", nil];
+ return [NSSet setWithObjects:@"mapID", nil];
}
- (NSURL *)styleURL
@@ -253,6 +247,13 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
}
_mbglMap->resize(self.bounds.size.width, self.bounds.size.height, _glView.contentScaleFactor);
+ // Observe for changes to the global access token (and find out the current one).
+ [[MGLAccountManager sharedManager] addObserver:self
+ forKeyPath:@"accessToken"
+ options:(NSKeyValueObservingOptionInitial |
+ NSKeyValueObservingOptionNew)
+ context:NULL];
+
// Notify map object when network reachability status changes.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
@@ -393,6 +394,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
[_regionChangeDelegateQueue cancelAllOperations];
[[NSNotificationCenter defaultCenter] removeObserver:self];
+ [[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];
if (_mbglMap)
{
@@ -1274,6 +1276,16 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
#pragma mark - Properties -
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(__unused void *)context
+{
+ // Synchronize mbgl::Map’s access token with the global one in MGLAccountManager.
+ if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager])
+ {
+ NSString *accessToken = change[NSKeyValueChangeNewKey];
+ _mbglMap->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
+ }
+}
+
+ (NSSet *)keyPathsForValuesAffectingZoomEnabled
{
return [NSSet setWithObject:@"allowsZooming"];
@@ -1535,7 +1547,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
+ (NSSet *)keyPathsForValuesAffectingMapID
{
- return [NSSet setWithObjects:@"styleURL", @"accessToken", nil];
+ return [NSSet setWithObjects:@"styleURL", nil];
}
- (NSString *)mapID
@@ -2464,7 +2476,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
self.layer.borderColor = [UIColor colorWithWhite:184/255. alpha:1].CGColor;
self.layer.borderWidth = 1;
- if (self.accessToken)
+ if ([MGLAccountManager accessToken])
{
self.layer.backgroundColor = [UIColor colorWithRed:59/255.
green:178/255.
@@ -2529,7 +2541,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
// More explanation
UILabel *explanationLabel2 = [[UILabel alloc] init];
- explanationLabel2.text = @"and enter it into the Access Token field in the Attributes inspector.";
+ explanationLabel2.text = @"and set it as the value of MGLMapboxAccessToken in the Info.plist file.";
explanationLabel2.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
explanationLabel2.numberOfLines = 0;
explanationLabel2.translatesAutoresizingMaskIntoConstraints = NO;
diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m
index 011040097a..a0e771ad07 100644
--- a/platform/ios/MGLMapboxEvents.m
+++ b/platform/ios/MGLMapboxEvents.m
@@ -177,8 +177,10 @@ const NSTimeInterval MGLFlushInterval = 60;
+ (void)initialize {
if (self == [MGLMapboxEvents class]) {
+ NSBundle *bundle = [NSBundle bundleForClass:self];
+ NSNumber *accountTypeNumber = [bundle objectForInfoDictionaryKey:@"MGLMapboxAccountType"];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
- @"MGLMapboxAccountType": @0,
+ @"MGLMapboxAccountType": accountTypeNumber ? accountTypeNumber : @0,
@"MGLMapboxMetricsEnabled": @YES,
}];
}
@@ -214,7 +216,7 @@ const NSTimeInterval MGLFlushInterval = 60;
}
}
- 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.");
+ 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 MGLMapboxMetricsEnabledSettingShownInApp to YES in Info.plist.");
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"MGLMapboxMetricsEnabled": defaultEnabledValue,
}];