diff options
Diffstat (limited to 'platform/osx/sdk/MGLAccountManager.m')
-rw-r--r-- | platform/osx/sdk/MGLAccountManager.m | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/platform/osx/sdk/MGLAccountManager.m b/platform/osx/sdk/MGLAccountManager.m new file mode 100644 index 0000000000..d6bc82defe --- /dev/null +++ b/platform/osx/sdk/MGLAccountManager.m @@ -0,0 +1,66 @@ +#import "MGLAccountManager_Private.h" + +#import <mbgl/osx/MGLMapView.h> + +#import "NSBundle+MGLAdditions.h" +#import "NSProcessInfo+MGLAdditions.h" +#import "../../darwin/NSString+MGLAdditions.h" + +@interface MGLAccountManager () + +@property (atomic) NSString *accessToken; + +@end + +@implementation MGLAccountManager + +#pragma mark - Internal + ++ (void)load { + mgl_linkBundleCategory(); + mgl_linkStringCategory(); + mgl_linkProcessInfoCategory(); + + [MGLMapView restorableStateKeyPaths]; + + // Read the initial configuration from Info.plist. + NSString *accessToken = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAccessToken"]; + if (accessToken.length) { + self.accessToken = accessToken; + } +} + +// Can be called from any thread. +// ++ (instancetype)sharedManager { + static dispatch_once_t onceToken; + static MGLAccountManager *_sharedManager; + void (^setupBlock)() = ^{ + dispatch_once(&onceToken, ^{ + _sharedManager = [[self alloc] init]; + }); + }; + if (![[NSThread currentThread] isMainThread]) { + dispatch_sync(dispatch_get_main_queue(), ^{ + setupBlock(); + }); + } + else { + setupBlock(); + } + return _sharedManager; +} + ++ (void)setAccessToken:(NSString *)accessToken { + accessToken = [accessToken stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if (![accessToken length]) return; + + [MGLAccountManager sharedManager].accessToken = accessToken; +} + ++ (NSString *)accessToken { + return [MGLAccountManager sharedManager].accessToken; +} + +@end |