summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-05-20 14:38:36 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-05-20 14:38:36 -0700
commit256b748458571201fe2b64bdf84dae6ec9b5932e (patch)
tree850d298f43f52db5e7e59eaabcb78a5d71237d0b /platform
parent8f8631963b66a0442e6b5fbc920a58d72e7ae1d7 (diff)
parent90a50c0c4150080f725cc6b66909eef58a2a9b4a (diff)
downloadqtlocation-mapboxgl-256b748458571201fe2b64bdf84dae6ec9b5932e.tar.gz
Merge branch '1ec5-plist-1535'
Diffstat (limited to 'platform')
-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
4 files changed, 55 insertions, 20 deletions
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,
}];