-
Notifications
You must be signed in to change notification settings - Fork 1.3k
MapboxGL Startup Infrastructure #1329
Changes from 3 commits
578d48b
19b5ef3
7e14578
317be34
591c632
f960461
0e36bf1
7f6cc6c
4ce4e97
804c092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: A common strategy for user defaults keys is to make them match the names of the constants that define them. So you’d define a constant called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd be better off putting things in a constant to avoid future potential problems with the |
||
} 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you’re creating a |
||
|
||
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | ||
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[MBXViewController new]]; | ||
[self.window makeKeyAndVisible]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#import "SMCalloutView.h" | ||
|
||
#import "MGLMapboxEvents.h" | ||
#import "MapboxGL.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import statement likle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import statement likely makes some of the other ones redundant. |
||
|
||
#import <algorithm> | ||
|
||
|
@@ -122,6 +123,7 @@ - (instancetype)initWithFrame:(CGRect)frame | |
if (self && [self commonInit]) | ||
{ | ||
self.styleURL = nil; | ||
self.accessToken = [MapboxGL getAccessToken]; | ||
return self; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#import "MapboxGL.h" | ||
#import "NSProcessInfo+MGLAdditions.h" | ||
|
||
@interface MapboxGL() | ||
|
||
@property (atomic) NSString *accessToken; | ||
|
||
@end | ||
|
||
|
||
@implementation MapboxGL | ||
|
||
static MapboxGL *_sharedManager; | ||
|
||
// Can be called from any thread. Called implicitly from any | ||
// public class convenience methods. | ||
// | ||
+ (id) sharedInstanceWithAccessToken:(NSString *)token { | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { | ||
void (^setupBlock)() = ^{ | ||
_sharedManager = [[self alloc] init]; | ||
_sharedManager.accessToken = token; | ||
}; | ||
if ( ! [[NSThread currentThread] isMainThread]) { | ||
dispatch_sync(dispatch_get_main_queue(), ^{ | ||
setupBlock(); | ||
}); | ||
} | ||
else { | ||
setupBlock(); | ||
} | ||
} | ||
}); | ||
return _sharedManager; | ||
} | ||
|
||
+ (NSString *) getAccessToken { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought, if the developer can just get the access token from a class method ( Let’s strive for parallelism in one of two ways:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ended up going with just renaming to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (_sharedManager) { | ||
return _sharedManager.accessToken; | ||
} | ||
return nil; | ||
} | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This header was meant to be an umbrella header, which does nothing but import all the other public-facing headers. (A developer should only ever have to import the one umbrella header unless they’re doing something out of the ordinary.)
I’d move this class to another header and rename it
MGLAccountManager
or somesuch, sticking to the framework’s established prefix.