diff options
author | Justin R. Miller <incanus@users.noreply.github.com> | 2016-10-15 20:12:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-15 20:12:36 -0700 |
commit | 674304bfae3b8ed9625d0de2f59d5eedcffaf143 (patch) | |
tree | 32b1a3c08ab0046db5ad1db269c8fa36963176c4 /platform/darwin/src | |
parent | e5a473a30181b8790743f12e1a4f7a0e300eb8b4 (diff) | |
download | qtlocation-mapboxgl-674304bfae3b8ed9625d0de2f59d5eedcffaf143.tar.gz |
[ios, macos] fixes #6346: add support for configurable API base URL (#6709)
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/MGLNetworkConfiguration.h | 11 | ||||
-rw-r--r-- | platform/darwin/src/MGLNetworkConfiguration.m | 43 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 16 |
3 files changed, 70 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLNetworkConfiguration.h b/platform/darwin/src/MGLNetworkConfiguration.h new file mode 100644 index 0000000000..4e22fa294c --- /dev/null +++ b/platform/darwin/src/MGLNetworkConfiguration.h @@ -0,0 +1,11 @@ +#import <Foundation/Foundation.h> + +@interface MGLNetworkConfiguration : NSObject + +/// Returns the shared instance of the `MGLNetworkConfiguration` class. ++ (instancetype)sharedManager; + +/// The current API base URL. +@property (atomic) NSURL *apiBaseURL; + +@end diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m new file mode 100644 index 0000000000..d661d9090e --- /dev/null +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -0,0 +1,43 @@ +#import "MGLNetworkConfiguration.h" +#import "NSProcessInfo+MGLAdditions.h" + +@implementation MGLNetworkConfiguration + ++ (void)load { + // Read the initial configuration from Info.plist. + NSString *apiBaseURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAPIBaseURL"]; + if (apiBaseURL.length) { + [self setAPIBaseURL:[NSURL URLWithString:apiBaseURL]]; + } +} + ++ (instancetype)sharedManager { + if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + return nil; + } + static dispatch_once_t onceToken; + static MGLNetworkConfiguration *_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)setAPIBaseURL:(NSURL *)apiBaseURL { + [MGLNetworkConfiguration sharedManager].apiBaseURL = apiBaseURL; +} + ++ (NSURL *)apiBaseURL { + return [MGLNetworkConfiguration sharedManager].apiBaseURL; +} + +@end diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index f279235ca0..5f284b76a1 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -2,6 +2,7 @@ #import "MGLAccountManager_Private.h" #import "MGLGeometry_Private.h" +#import "MGLNetworkConfiguration.h" #import "MGLOfflinePack_Private.h" #import "MGLOfflineRegion_Private.h" #import "MGLTilePyramidOfflineRegion.h" @@ -127,6 +128,13 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = @"MaximumCount"; _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); + // Observe for changes to the API base URL (and find out the current one). + [[MGLNetworkConfiguration sharedManager] addObserver:self + forKeyPath:@"apiBaseURL" + options:(NSKeyValueObservingOptionInitial | + NSKeyValueObservingOptionNew) + context:NULL]; + // Observe for changes to the global access token (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self forKeyPath:@"accessToken" @@ -147,6 +155,7 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = @"MaximumCount"; } - (void)dealloc { + [[MGLNetworkConfiguration sharedManager] removeObserver:self forKeyPath:@"apiBaseURL"]; [[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"]; for (MGLOfflinePack *pack in self.packs) { @@ -164,6 +173,13 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = @"MaximumCount"; if (![accessToken isKindOfClass:[NSNull class]]) { self.mbglFileSource->setAccessToken(accessToken.UTF8String); } + } else if ([keyPath isEqualToString:@"apiBaseURL"] && object == [MGLNetworkConfiguration sharedManager]) { + NSURL *apiBaseURL = change[NSKeyValueChangeNewKey]; + if ([apiBaseURL isKindOfClass:[NSNull class]]) { + self.mbglFileSource->setAPIBaseURL(mbgl::util::API_BASE_URL); + } else { + self.mbglFileSource->setAPIBaseURL(apiBaseURL.absoluteString.UTF8String); + } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } |