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

Commit

Permalink
#1225 - Simplifying MGLAccountManager for initial setup and access to…
Browse files Browse the repository at this point in the history
… AccessToken
  • Loading branch information
bleege committed May 5, 2015
1 parent 4ce4e97 commit 804c092
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/mbgl/ios/MGLAccountManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@interface MGLAccountManager : NSObject

+ (instancetype) sharedInstanceWithAccessToken:(NSString *)token;
+ (NSString *) sharedAccessToken;
+ (void) setAccessToken:(NSString *) accessToken;
+ (NSString *) accessToken;

@end
3 changes: 2 additions & 1 deletion ios/app/MBXAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}
if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work.");

[MGLAccountManager sharedInstanceWithAccessToken:accessToken];
// Start Mapbox GL SDK
[MGLAccountManager setAccessToken:accessToken];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[MBXViewController new]];
Expand Down
23 changes: 13 additions & 10 deletions platform/ios/MGLAccountManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ @implementation MGLAccountManager

static MGLAccountManager *_sharedManager;

// Can be called from any thread. Called implicitly from any
// public class convenience methods.
// Can be called from any thread.
//
+ (instancetype) sharedInstanceWithAccessToken:(NSString *)token {
+ (instancetype) sharedInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
void (^setupBlock)() = ^{
_sharedManager = [[self alloc] init];
_sharedManager.accessToken = token;
[MGLMapboxEvents setToken:token];
};
if ( ! [[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
Expand All @@ -40,11 +37,17 @@ + (instancetype) sharedInstanceWithAccessToken:(NSString *)token {
return _sharedManager;
}

+ (NSString *) sharedAccessToken {
if (_sharedManager) {
return _sharedManager.accessToken;
}
return nil;
+ (void) setAccessToken:(NSString *) accessToken {
[[MGLAccountManager sharedInstance] setAccessToken:accessToken];

// Update MGLMapboxEvents
// NOTE: This is (likely) the initial setup of MGLMapboxEvents
[MGLMapboxEvents setToken:accessToken];
}

+ (NSString *) accessToken {
return [MGLAccountManager sharedInstance].accessToken;
}


@end
2 changes: 1 addition & 1 deletion platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self && [self commonInit])
{
self.styleURL = nil;
self.accessToken = [MGLAccountManager sharedAccessToken];
self.accessToken = [MGLAccountManager accessToken];
return self;
}

Expand Down

0 comments on commit 804c092

Please sign in to comment.