[ios] Enable/Disable sku token handling (#14467)
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
#import "MBXSKUToken.h"
|
||||
#endif
|
||||
|
||||
static BOOL _MGLAccountsSDKEnabled;
|
||||
|
||||
@interface MGLAccountManager ()
|
||||
|
||||
@property (atomic) NSString *accessToken;
|
||||
@@ -19,7 +21,6 @@
|
||||
@property (atomic) NSString *skuToken;
|
||||
@property (atomic) NSDate *skuTokenExpiration;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
@implementation MGLAccountManager
|
||||
@@ -32,7 +33,7 @@
|
||||
if (accessToken.length) {
|
||||
self.accessToken = accessToken;
|
||||
}
|
||||
|
||||
|
||||
NSString *apiBaseURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAPIBaseURL"];
|
||||
|
||||
// If apiBaseURL is not a valid URL, [NSURL URLWithString:] will be `nil`.
|
||||
@@ -41,7 +42,17 @@
|
||||
}
|
||||
|
||||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
|
||||
self.skuToken = [MBXSKUToken skuToken];
|
||||
// TODO: Use MGL_OBJC_DYNAMIC_CAST (that requires moving the macro, where it
|
||||
// doesn't require a C++ header)
|
||||
NSNumber *accountsSDKNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAccountsSDKEnabled"];
|
||||
if ([accountsSDKNumber isKindOfClass:[NSNumber class]]) {
|
||||
_MGLAccountsSDKEnabled = ((NSNumber*)accountsSDKNumber).boolValue;
|
||||
}
|
||||
|
||||
if (self.isAccountsSDKEnabled) {
|
||||
self.skuToken = MBXSKUToken.mapsToken;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -101,15 +112,31 @@
|
||||
|
||||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
|
||||
|
||||
+ (void)setSkuToken:(NSString *)skuToken {
|
||||
NSTimeInterval oneHour = 60 * 60; // TODO: make this const
|
||||
MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate dateWithTimeIntervalSinceNow:oneHour];
|
||||
+ (BOOL)isAccountsSDKEnabled {
|
||||
return _MGLAccountsSDKEnabled;
|
||||
}
|
||||
|
||||
MGLAccountManager.sharedManager.skuToken = skuToken;
|
||||
+ (void)setSkuToken:(NSString *)skuToken {
|
||||
if (MGLAccountManager.isAccountsSDKEnabled) {
|
||||
NSTimeInterval oneHour = 60 * 60; // TODO: make this const
|
||||
MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate dateWithTimeIntervalSinceNow:oneHour];
|
||||
MGLAccountManager.sharedManager.skuToken = skuToken;
|
||||
}
|
||||
else {
|
||||
MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate distantFuture];
|
||||
MGLAccountManager.sharedManager.skuToken = nil;
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSString *)skuToken {
|
||||
return [MGLAccountManager.sharedManager isSKUTokenExpired] ? [MBXSKUToken skuToken] : MGLAccountManager.sharedManager.skuToken;
|
||||
if (MGLAccountManager.isAccountsSDKEnabled) {
|
||||
return [MGLAccountManager.sharedManager isSKUTokenExpired] ?
|
||||
MBXSKUToken.mapsToken :
|
||||
MGLAccountManager.sharedManager.skuToken;
|
||||
}
|
||||
else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isSKUTokenExpired {
|
||||
|
||||
@@ -15,7 +15,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
|
||||
/// The current global SKU.
|
||||
@property (class, atomic, readonly) NSString *skuToken;
|
||||
@property (class, atomic, readonly, nullable) NSString *skuToken;
|
||||
@property (class, nonatomic, readonly) BOOL isAccountsSDKEnabled;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
@@ -208,10 +208,17 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
if (impl->accountType == 0 &&
|
||||
([url.host isEqualToString:@"mapbox.com"] || [url.host hasSuffix:@".mapbox.com"])) {
|
||||
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
|
||||
NSArray *newQueryItems = @[
|
||||
[NSURLQueryItem queryItemWithName:@"events" value:@"true"],
|
||||
[NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken]
|
||||
];
|
||||
|
||||
NSMutableArray *newQueryItems = [NSMutableArray arrayWithArray:@[
|
||||
[NSURLQueryItem queryItemWithName:@"events" value:@"true"]
|
||||
]];
|
||||
|
||||
// Only add the token if we have enabled the accounts SDK
|
||||
if (MGLAccountManager.isAccountsSDKEnabled)
|
||||
{
|
||||
NSCAssert(MGLAccountManager.skuToken, @"skuToken should be non-nil if the accounts SDK is enabled");
|
||||
[newQueryItems addObject:[NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken]];
|
||||
}
|
||||
|
||||
components.queryItems = components.queryItems ? [components.queryItems arrayByAddingObjectsFromArray:newQueryItems] : newQueryItems;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#import "MGLMapboxEvents.h"
|
||||
#import "MBXSKUToken.h"
|
||||
#import "NSBundle+MGLAdditions.h"
|
||||
#import "MGLAccountManager_Private.h"
|
||||
|
||||
static NSString * const MGLAPIClientUserAgentBase = @"mapbox-maps-ios";
|
||||
static NSString * const MGLMapboxAccountType = @"MGLMapboxAccountType";
|
||||
@@ -144,7 +145,9 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence";
|
||||
[[MGLMapboxEvents sharedInstance] eventsManager].baseURL = [MGLMapboxEvents sharedInstance].baseURL;
|
||||
}
|
||||
|
||||
[[MGLMapboxEvents sharedInstance] eventsManager].skuId = MBXAccountsSKUIDMaps;
|
||||
if (MGLAccountManager.isAccountsSDKEnabled) {
|
||||
[[self sharedInstance] eventsManager].skuId = MBXAccountsMapsSKUIDMaps;
|
||||
}
|
||||
|
||||
[self flush];
|
||||
});
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
extern NSString *const MBXAccountsSKUIDMaps;
|
||||
extern NSString *const MBXAccountsMapsSKUIDMaps;
|
||||
extern NSString *const MBXAccountsNavigationSKUIDMaps;
|
||||
|
||||
@interface MBXSKUToken : NSObject
|
||||
|
||||
+ (NSString *)skuToken;
|
||||
@property (class, nonatomic, readonly) NSString *mapsToken;
|
||||
@property (class, nonatomic, readonly) NSString *navigationToken;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user