[ios] Enable SKU tokens for all requests

Removes conditional code that relied on a plist flag. Other small cleanups.
This commit is contained in:
Jason Wray
2019-05-20 22:49:50 -07:00
committed by GitHub
parent 3cd8c6c1fd
commit 29140f5b11
5 changed files with 11 additions and 51 deletions

View File

@@ -10,7 +10,7 @@
#import "MBXSKUToken.h"
#endif
static BOOL _MGLAccountsSDKEnabled;
static const NSTimeInterval MGLAccountManagerSKUTokenLifespan = 3600;
@interface MGLAccountManager ()
@@ -42,17 +42,7 @@ static BOOL _MGLAccountsSDKEnabled;
}
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
// 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 tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser];
}
self.skuToken = [MBXSKUToken tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser];
#endif
}
@@ -112,35 +102,19 @@ static BOOL _MGLAccountsSDKEnabled;
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
+ (BOOL)isAccountsSDKEnabled {
return _MGLAccountsSDKEnabled;
}
+ (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;
}
MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate dateWithTimeIntervalSinceNow:MGLAccountManagerSKUTokenLifespan];
MGLAccountManager.sharedManager.skuToken = skuToken;
}
+ (NSString *)skuToken {
if (MGLAccountManager.isAccountsSDKEnabled) {
return [MGLAccountManager.sharedManager isSKUTokenExpired] ?
[MBXSKUToken tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser] :
MGLAccountManager.sharedManager.skuToken;
}
else {
return nil;
}
return [MGLAccountManager.sharedManager isSKUTokenExpired] ?
[MBXSKUToken tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser] :
MGLAccountManager.sharedManager.skuToken;
}
- (BOOL)isSKUTokenExpired {
NSTimeInterval secondsUntilExpiration = [MGLAccountManager.sharedManager.skuTokenExpiration timeIntervalSinceDate:NSDate.date];
NSTimeInterval secondsUntilExpiration = [MGLAccountManager.sharedManager.skuTokenExpiration timeIntervalSinceNow];
return secondsUntilExpiration < 0;
}

View File

@@ -15,8 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
/// The current global SKU.
@property (class, atomic, readonly, nullable) NSString *skuToken;
@property (class, nonatomic, readonly) BOOL isAccountsSDKEnabled;
@property (class, atomic, readonly) NSString *skuToken;
#endif
@end

View File

@@ -208,16 +208,7 @@ 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];
NSURLQueryItem *accountsQueryItem = nil;
// 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");
accountsQueryItem = [NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken];
} else {
accountsQueryItem = [NSURLQueryItem queryItemWithName:@"events" value:@"true"];
}
NSURLQueryItem *accountsQueryItem = [NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken];
components.queryItems = components.queryItems ? [components.queryItems arrayByAddingObject:accountsQueryItem] : @[accountsQueryItem];
url = components.URL;
}

View File

@@ -66,7 +66,5 @@
<string>settings</string>
</dict>
</array>
<key>MGLMapboxAccountsSDKEnabled</key>
<true/>
</dict>
</plist>

View File

@@ -145,9 +145,7 @@ static NSString * const MGLVariableGeofence = @"VariableGeofence";
[[MGLMapboxEvents sharedInstance] eventsManager].baseURL = [MGLMapboxEvents sharedInstance].baseURL;
}
if (MGLAccountManager.isAccountsSDKEnabled) {
[[self sharedInstance] eventsManager].skuId = MBXAccountsSKUIDMaps;
}
[[self sharedInstance] eventsManager].skuId = MBXAccountsSKUIDMaps;
[self flush];
});