#1225 - Initial conversion of Mapbox GL app to set access token in AppDelegate and exposing MGLMapView.initWithFrame to support it

This commit is contained in:
Brad Leege
2015-04-23 18:15:01 -05:00
parent 19b5ef367e
commit 7e145780eb
6 changed files with 38 additions and 18 deletions

View File

@@ -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.

View File

@@ -5,6 +5,7 @@
@interface MapboxGL : NSObject
+ (void) sharedInstanceWithAccessToken:(NSString *)token;
+ (id) sharedInstanceWithAccessToken:(NSString *)token;
+ (NSString *) getAccessToken;
@end

View File

@@ -1,11 +1,27 @@
#import "MBXAppDelegate.h"
#import "MBXViewController.h"
#import <mbgl/ios/MapboxGL.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.");
[MapboxGL sharedInstanceWithAccessToken:accessToken];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[MBXViewController new]];
[self.window makeKeyAndVisible];

View File

@@ -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.viewControllerForLayoutGuides = self;
self.mapView.showsUserLocation = YES;

View File

@@ -25,6 +25,7 @@
#import "SMCalloutView.h"
#import "MGLMapboxEvents.h"
#import "MapboxGL.h"
#import <algorithm>
@@ -122,6 +123,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
if (self && [self commonInit])
{
self.styleURL = nil;
self.accessToken = [MapboxGL getAccessToken];
return self;
}

View File

@@ -1,22 +1,24 @@
#import <Foundation/Foundation.h>
#import "MapboxGL.h"
#import "NSProcessInfo+MGLAdditions.h"
@interface MapboxGL()
@property (atomic) NSString *accessToken;
@end
@property (atomic) NSString *accessToken;
@implementation MapboxGL
static MapboxGL *_sharedManager;
// Can be called from any thread. Called implicitly from any
// public class convenience methods.
//
+ (instancetype) sharedInstanceWithAccessToken:(NSString *)token {
+ (id) sharedInstanceWithAccessToken:(NSString *)token {
static dispatch_once_t onceToken;
static MapboxGL *_sharedManager;
dispatch_once(&onceToken, ^{
if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
void (^setupBlock)() = ^{
@@ -36,4 +38,11 @@
return _sharedManager;
}
+ (NSString *) getAccessToken {
if (_sharedManager) {
return _sharedManager.accessToken;
}
return nil;
}
@end