Files
MapLibreParticleLab/platform/darwin/src/MGLAccountManager.m
Jordan Kiley 5d85056030 [ios, macos] Move apiBaseURL to MGLAccountManager` (#13754)
[ios, macos] Move `apiBaseURL` to `MGLAccountManager`
2019-02-13 13:20:48 -08:00

92 lines
2.4 KiB
Objective-C

#import "MGLAccountManager_Private.h"
#import "NSBundle+MGLAdditions.h"
#import "NSProcessInfo+MGLAdditions.h"
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#import "MGLMapboxEvents.h"
@interface MGLAccountManager ()
@property (atomic) NSString *accessToken;
@property (nonatomic) NSURL *apiBaseURL;
@end
#else
@interface MGLAccountManager ()
@property (atomic) NSString *accessToken;
@property (nonatomic) NSURL *apiBaseURL;
@end
#endif
@implementation MGLAccountManager
#pragma mark - Internal
+ (void)load {
// Read the initial configuration from Info.plist.
NSString *accessToken = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAccessToken"];
if (accessToken.length) {
self.accessToken = accessToken;
}
NSString *apiBaseURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAPIBaseURL"];
// If apiBaseURL is not a valid URL, [NSURL URLWithString:] will be `nil`.
if (apiBaseURL.length && [NSURL URLWithString:apiBaseURL]) {
[self setAPIBaseURL:[NSURL URLWithString:apiBaseURL]];
}
}
+ (instancetype)sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
return nil;
}
static dispatch_once_t onceToken;
static MGLAccountManager *_sharedManager;
void (^setupBlock)(void) = ^{
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;
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
dispatch_async(dispatch_get_main_queue(), ^{
[MGLMapboxEvents setupWithAccessToken:accessToken];
});
#endif
}
+ (NSString *)accessToken {
return [MGLAccountManager sharedManager].accessToken;
}
+ (void)setAPIBaseURL:(NSURL *)apiBaseURL {
[MGLAccountManager sharedManager].apiBaseURL = apiBaseURL;
}
+ (NSURL *)apiBaseURL {
return [MGLAccountManager sharedManager].apiBaseURL;
}
@end