diff options
author | Brad Leege <bleege@gmail.com> | 2015-05-05 13:00:25 -0500 |
---|---|---|
committer | Brad Leege <bleege@gmail.com> | 2015-05-05 13:00:25 -0500 |
commit | acd27fc386ba413fbec4cac75d4407eb77712883 (patch) | |
tree | c227dd491369473ad25f0faf4018921df5f89d1c | |
parent | 97fe50f35dbaa653fce44aa2918ba20b65c6b704 (diff) | |
parent | 804c092c81c01a4ba9bc25654c30f5f285e6eb54 (diff) | |
download | qtlocation-mapboxgl-acd27fc386ba413fbec4cac75d4407eb77712883.tar.gz |
Merge pull request #1329 from mapbox/1225-start-token
MapboxGL Startup Infrastructure
-rw-r--r-- | gyp/platform-ios.gypi | 2 | ||||
-rw-r--r-- | include/mbgl/ios/MGLAccountManager.h | 6 | ||||
-rw-r--r-- | include/mbgl/ios/MGLMapView.h | 7 | ||||
-rw-r--r-- | include/mbgl/ios/MapboxGL.h | 1 | ||||
-rw-r--r-- | ios/app/MBXAppDelegate.m | 18 | ||||
-rw-r--r-- | ios/app/MBXViewController.mm | 15 | ||||
-rw-r--r-- | platform/ios/MGLAccountManager.m | 53 | ||||
-rw-r--r-- | platform/ios/MGLMapView.mm | 2 |
8 files changed, 88 insertions, 16 deletions
diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index d23192c8c6..1f96f94702 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -25,6 +25,8 @@ '../platform/ios/MGLMapView.mm', '../platform/ios/MGLFileCache.h', '../platform/ios/MGLFileCache.mm', + '../include/mbgl/ios/MGLAccountManager.h', + '../platform/ios/MGLAccountManager.m', '../include/mbgl/ios/MGLAnnotation.h', '../include/mbgl/ios/MGLUserLocation.h', '../platform/ios/MGLUserLocation_Private.h', diff --git a/include/mbgl/ios/MGLAccountManager.h b/include/mbgl/ios/MGLAccountManager.h new file mode 100644 index 0000000000..c1b7406b9e --- /dev/null +++ b/include/mbgl/ios/MGLAccountManager.h @@ -0,0 +1,6 @@ +@interface MGLAccountManager : NSObject + ++ (void) setAccessToken:(NSString *) accessToken; ++ (NSString *) accessToken; + +@end
\ No newline at end of file diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h index 7c757ba93c..9d7b5c9019 100644 --- a/include/mbgl/ios/MGLMapView.h +++ b/include/mbgl/ios/MGLMapView.h @@ -20,6 +20,11 @@ IB_DESIGNABLE /** @name Initializing a Map View */ +/** Initialize a map view with the default style, given frame, and access token set in MapboxGL singleton. +* @param frame The frame with which to initialize the map view. +* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ +- (instancetype)initWithFrame:(CGRect)frame; + /** Initialize a map view with the default style and a given frame and access token. * @param frame The frame with which to initialize the map view. * @param accessToken A Mapbox API access token. @@ -33,8 +38,6 @@ IB_DESIGNABLE * @return An initialized map view, or `nil` if the map view was unable to be initialized. */ - (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL; -- (instancetype)initWithFrame:(CGRect)frame __attribute__((unavailable("Instantiating an MGLMapView requires setting a style and/or an access token."))); - #pragma mark - Authorizing Access /** @name Authorizing Access */ diff --git a/include/mbgl/ios/MapboxGL.h b/include/mbgl/ios/MapboxGL.h index c32915bc76..34e080a511 100644 --- a/include/mbgl/ios/MapboxGL.h +++ b/include/mbgl/ios/MapboxGL.h @@ -2,3 +2,4 @@ #import "MGLMapView.h" #import "MGLTypes.h" #import "MGLUserLocation.h" +#import "MGLAccountManager.h"
\ No newline at end of file diff --git a/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m index f2ef69ec63..90c552aba0 100644 --- a/ios/app/MBXAppDelegate.m +++ b/ios/app/MBXAppDelegate.m @@ -1,11 +1,29 @@ #import "MBXAppDelegate.h" #import "MBXViewController.h" +#import <mbgl/ios/MapboxGL.h> +#import <mbgl/ios/MGLAccountManager.h> #import <mbgl/ios/MGLMapboxEvents.h> @implementation MBXAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Set Access Token + NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"]; + if (accessToken) { + // Store to preferences so that we can launch the app later on without having to specify + // token. + [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"]; + } else { + // Try to retrieve from preferences, maybe we've stored them there previously and can reuse + // the token. + accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]; + } + if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work."); + + // Start Mapbox GL SDK + [MGLAccountManager setAccessToken:accessToken]; + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[MBXViewController new]]; [self.window makeKeyAndVisible]; diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm index 98c8e016db..eb77a2ec80 100644 --- a/ios/app/MBXViewController.mm +++ b/ios/app/MBXViewController.mm @@ -52,20 +52,7 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; { [super viewDidLoad]; - NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"]; - if (accessToken) { - // Store to preferences so that we can launch the app later on without having to specify - // token. - [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"]; - } else { - // Try to retrieve from preferences, maybe we've stored them there previously and can reuse - // the token. - accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]; - } - - if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work."); - - self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds accessToken:accessToken]; + self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds]; self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.mapView.showsUserLocation = YES; self.mapView.delegate = self; diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m new file mode 100644 index 0000000000..b1a80fd6a7 --- /dev/null +++ b/platform/ios/MGLAccountManager.m @@ -0,0 +1,53 @@ +#import <Foundation/Foundation.h> + +#import "MGLAccountManager.h" +#import "NSProcessInfo+MGLAdditions.h" +#import "MGLMapboxEvents.h" + +@interface MGLAccountManager() + +@property (atomic) NSString *accessToken; + +@end + + +@implementation MGLAccountManager + +static MGLAccountManager *_sharedManager; + +// Can be called from any thread. +// ++ (instancetype) sharedInstance { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + void (^setupBlock)() = ^{ + _sharedManager = [[self alloc] init]; + }; + if ( ! [[NSThread currentThread] isMainThread]) { + dispatch_sync(dispatch_get_main_queue(), ^{ + setupBlock(); + }); + } + else { + setupBlock(); + } + } + }); + return _sharedManager; +} + ++ (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
\ No newline at end of file diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 6c48755463..53c7526602 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -28,6 +28,7 @@ #import "SMCalloutView.h" #import "MGLMapboxEvents.h" +#import "MapboxGL.h" #import <algorithm> @@ -109,6 +110,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) if (self && [self commonInit]) { self.styleURL = nil; + self.accessToken = [MGLAccountManager accessToken]; return self; } |