Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
#1225 - Initial conversion of Mapbox GL app to set access token in Ap…
Browse files Browse the repository at this point in the history
…pDelegate and exposing MGLMapView.initWithFrame to support it
  • Loading branch information
bleege committed Apr 23, 2015
1 parent 19b5ef3 commit 7e14578
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
5 changes: 5 additions & 0 deletions include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

This comment has been minimized.

Copy link
@1ec5

1ec5 Apr 24, 2015

Contributor

It’s unnecessary to redeclare -initWithFrame: here, because the superclass, UIView, already declares -initWithFrame:.

This comment has been minimized.

Copy link
@bleege

bleege Apr 24, 2015

Author Contributor

Yeah, I found that really odd too originally. Thanks for pointing this out though because on 2nd look now I found what was causing the it not to compile originally.

This comment has been minimized.

Copy link
@bleege

bleege Apr 24, 2015

Author Contributor

I just removed the unavailable line which also would have let me remove - (instancetype)initWithFrame:(CGRect)frame; too, but I decided to leave it in as provides a placeholder for API documentation and it technically is specific to MGLMapView's implementation now and not just a pass through to UIView.


/** 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.
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/ios/MapboxGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@interface MapboxGL : NSObject

+ (void) sharedInstanceWithAccessToken:(NSString *)token;
+ (id) sharedInstanceWithAccessToken:(NSString *)token;
+ (NSString *) getAccessToken;

@end
16 changes: 16 additions & 0 deletions ios/app/MBXAppDelegate.m
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"];
} 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];
Expand Down
15 changes: 1 addition & 14 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,7 @@ - (void)viewDidLoad
{
[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;
Expand Down
2 changes: 2 additions & 0 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "SMCalloutView.h"

#import "MGLMapboxEvents.h"
#import "MapboxGL.h"

#import <algorithm>

Expand Down Expand Up @@ -122,6 +123,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self && [self commonInit])
{
self.styleURL = nil;
self.accessToken = [MapboxGL getAccessToken];
return self;
}

Expand Down
15 changes: 12 additions & 3 deletions platform/ios/MapboxGL.m
Original file line number Diff line number Diff line change
@@ -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 {

This comment has been minimized.

Copy link
@1ec5

1ec5 Apr 24, 2015

Contributor

instancetype is better (more specific) than id here.

This comment has been minimized.

Copy link
@bleege

bleege Apr 24, 2015

Author Contributor

Will do! 👍

static dispatch_once_t onceToken;
static MapboxGL *_sharedManager;
dispatch_once(&onceToken, ^{
if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
void (^setupBlock)() = ^{
Expand All @@ -36,4 +38,11 @@ + (instancetype) sharedInstanceWithAccessToken:(NSString *)token {
return _sharedManager;
}

+ (NSString *) getAccessToken {
if (_sharedManager) {
return _sharedManager.accessToken;
}
return nil;
}

@end

0 comments on commit 7e14578

Please sign in to comment.