Fixed race condition after setting access token
`Map` should be able to deal with having no access token or JSON even while it’s running. Most of `Map` accounts for this situation, but `reloadStyle()` incorrectly assumes that one or the other is set. This change corrects the assumption in `reloadStyle()`. Also expose the access token and style name to clients as ordinary KVO-compliant properties. Actually, they’re not so ordinary because they’re inspectable! Ref #1070, #1147
This commit is contained in:
@@ -30,6 +30,8 @@
|
||||
'../include/mbgl/ios/MGLMetricsLocationManager.h',
|
||||
'../platform/ios/MGLMetricsLocationManager.m',
|
||||
'../include/mbgl/ios/MGLTypes.h',
|
||||
'../platform/ios/NSString+MGLAdditions.h',
|
||||
'../platform/ios/NSString+MGLAdditions.m',
|
||||
'../platform/ios/vendor/SMCalloutView/SMCalloutView.h',
|
||||
'../platform/ios/vendor/SMCalloutView/SMCalloutView.m',
|
||||
],
|
||||
@@ -50,6 +52,7 @@
|
||||
'-framework MobileCoreServices',
|
||||
'-framework QuartzCore',
|
||||
'-framework SystemConfiguration',
|
||||
'-ObjC',
|
||||
],
|
||||
},
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
|
||||
/** Sets a Mapbox API access token for the map view.
|
||||
* @param accessToken A Mapbox API token. */
|
||||
- (void)setAccessToken:(NSString *)accessToken;
|
||||
@property (nonatomic) IBInspectable NSString *accessToken;
|
||||
|
||||
#pragma mark - Managing Constraints
|
||||
|
||||
@@ -194,11 +194,11 @@
|
||||
|
||||
/** Sets the map style to a named, bundled style.
|
||||
* @param styleName The map style name to use. */
|
||||
- (void)useBundledStyleNamed:(NSString *)styleName;
|
||||
@property (nonatomic) IBInspectable NSString *styleName;
|
||||
|
||||
/** Sets the map style URL to use.
|
||||
* @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://<user.style>`). */
|
||||
- (void)setStyleURL:(NSURL *)styleURL;
|
||||
@property (nonatomic) NSURL *styleURL;
|
||||
|
||||
#pragma mark - Annotating the Map
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ public:
|
||||
Duration getDefaultTransitionDuration();
|
||||
void setStyleURL(const std::string& url);
|
||||
void setStyleJSON(const std::string& json, const std::string& base = "");
|
||||
std::string getStyleURL() const;
|
||||
std::string getStyleJSON() const;
|
||||
|
||||
// Transition
|
||||
|
||||
@@ -19,7 +19,7 @@ static NSArray *const kStyleNames = @[
|
||||
@"Hybrid",
|
||||
];
|
||||
|
||||
static NSString *const kStyleVersion = @"v7";
|
||||
static NSString *const kStyleVersion = @"7";
|
||||
|
||||
@interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate>
|
||||
|
||||
@@ -246,10 +246,7 @@ mbgl::Settings_NSUserDefaults *settings = nullptr;
|
||||
styleName = [kStyleNames objectAtIndex:index];
|
||||
}
|
||||
|
||||
[self.mapView useBundledStyleNamed:
|
||||
[[[styleName lowercaseString]
|
||||
stringByAppendingString:@"-"]
|
||||
stringByAppendingString:kStyleVersion]];
|
||||
self.mapView.styleName = [NSString stringWithFormat:@"%@-v%@", styleName.lowercaseString, kStyleVersion];
|
||||
|
||||
[titleButton setTitle:styleName forState:UIControlStateNormal];
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <mbgl/util/geo.hpp>
|
||||
|
||||
#import "MGLTypes.h"
|
||||
#import "NSString+MGLAdditions.h"
|
||||
#import "MGLAnnotation.h"
|
||||
#import "MGLUserLocationAnnotationView.h"
|
||||
#import "MGLUserLocation_Private.h"
|
||||
@@ -43,7 +44,7 @@ const std::string &defaultCacheDatabase() {
|
||||
static dispatch_once_t loadGLExtensions;
|
||||
|
||||
NSString *const MGLDefaultStyleName = @"Emerald";
|
||||
NSString *const MGLStyleVersion = @"v7";
|
||||
NSString *const MGLStyleVersion = @"7";
|
||||
NSString *const MGLDefaultStyleMarkerSymbolName = @"default_marker";
|
||||
|
||||
const NSTimeInterval MGLAnimationDuration = 0.3;
|
||||
@@ -106,7 +107,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
|
||||
if (self && [self commonInit])
|
||||
{
|
||||
if (accessToken) [self setAccessToken:accessToken];
|
||||
[self setAccessToken:accessToken];
|
||||
|
||||
if (styleJSON || accessToken)
|
||||
{
|
||||
@@ -126,8 +127,8 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
|
||||
if (self && [self commonInit])
|
||||
{
|
||||
if (accessToken) [self setAccessToken:accessToken];
|
||||
if (styleName) [self useBundledStyleNamed:styleName];
|
||||
[self setAccessToken:accessToken];
|
||||
if (styleName) [self setStyleName:styleName];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -163,22 +164,22 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSString *)accessToken
|
||||
{
|
||||
return @(mbglMap->getAccessToken().c_str()).mgl_stringOrNilIfEmpty;
|
||||
}
|
||||
|
||||
- (void)setAccessToken:(NSString *)accessToken
|
||||
{
|
||||
if (accessToken)
|
||||
{
|
||||
mbglMap->setAccessToken((std::string)[accessToken UTF8String]);
|
||||
[MGLMapboxEvents setToken:accessToken];
|
||||
}
|
||||
mbglMap->setAccessToken((std::string)[accessToken UTF8String]);
|
||||
[MGLMapboxEvents setToken:accessToken.mgl_stringOrNilIfEmpty];
|
||||
}
|
||||
|
||||
- (void)setStyleJSON:(NSString *)styleJSON
|
||||
{
|
||||
if ( ! styleJSON)
|
||||
{
|
||||
[self useBundledStyleNamed:[[[MGLDefaultStyleName lowercaseString]
|
||||
stringByAppendingString:@"-"]
|
||||
stringByAppendingString:MGLStyleVersion]];
|
||||
[self setStyleName:[NSString stringWithFormat:@"%@-v%@", MGLDefaultStyleName.lowercaseString, MGLStyleVersion]];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -186,6 +187,12 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURL *)styleURL
|
||||
{
|
||||
NSString *styleURLString = @(mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty;
|
||||
return styleURLString ? [NSURL URLWithString:styleURLString] : nil;
|
||||
}
|
||||
|
||||
- (void)setStyleURL:(NSURL *)styleURL
|
||||
{
|
||||
std::string styleURLString([[styleURL absoluteString] UTF8String]);
|
||||
@@ -1322,7 +1329,21 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
|
||||
return [NSArray arrayWithArray:_bundledStyleNames];
|
||||
}
|
||||
|
||||
- (void)useBundledStyleNamed:(NSString *)styleName
|
||||
- (NSString *)styleName
|
||||
{
|
||||
NSURL *styleURL = self.styleURL;
|
||||
NSString *styleName;
|
||||
if ([styleURL.scheme isEqualToString:@"asset"])
|
||||
{
|
||||
styleName = styleURL.lastPathComponent.stringByDeletingPathExtension;
|
||||
}
|
||||
else if ([styleURL.scheme isEqualToString:@"mapbox"]) {
|
||||
styleName = styleURL.host;
|
||||
}
|
||||
return styleName.mgl_stringOrNilIfEmpty;
|
||||
}
|
||||
|
||||
- (void)setStyleName:(NSString *)styleName
|
||||
{
|
||||
NSString *hybridStylePrefix = @"hybrid-";
|
||||
BOOL isHybrid = [styleName hasPrefix:hybridStylePrefix];
|
||||
|
||||
8
platform/ios/NSString+MGLAdditions.h
Normal file
8
platform/ios/NSString+MGLAdditions.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSString (MGLAdditions)
|
||||
|
||||
/// Returns the receiver if non-empty or \c nil if empty.
|
||||
- (NSString *)mgl_stringOrNilIfEmpty;
|
||||
|
||||
@end
|
||||
10
platform/ios/NSString+MGLAdditions.m
Normal file
10
platform/ios/NSString+MGLAdditions.m
Normal file
@@ -0,0 +1,10 @@
|
||||
#import "NSString+MGLAdditions.h"
|
||||
|
||||
@implementation NSString (MGLAdditions)
|
||||
|
||||
- (NSString *)mgl_stringOrNilIfEmpty
|
||||
{
|
||||
return self.length ? self : nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -396,6 +396,10 @@ void Map::setup() {
|
||||
painter->setup();
|
||||
}
|
||||
|
||||
std::string Map::getStyleURL() const {
|
||||
return data->getStyleInfo().url;
|
||||
}
|
||||
|
||||
void Map::setStyleURL(const std::string &url) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
|
||||
@@ -756,7 +760,7 @@ void Map::reloadStyle() {
|
||||
Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
} else if (!styleInfo.json.empty()) {
|
||||
// We got JSON data directly.
|
||||
loadStyleJSON(styleInfo.json, styleInfo.base);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user