[ios] Update libmbxaccounts for navigation SDK override (#14802)

* Use a stringy selector... yerg.
* Bump to libmbxaccounts 1.0.0
This commit is contained in:
Jason Wray
2019-06-05 23:13:34 -07:00
committed by GitHub
parent 36361192b1
commit bd3d19f5bd
4 changed files with 38 additions and 30 deletions

View File

@@ -8,9 +8,10 @@
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#import "MGLMapboxEvents.h"
#import "MBXSKUToken.h"
#endif
static const NSTimeInterval MGLAccountManagerSKUTokenLifespan = 3600;
static NSString * const MGLAccountManagerExternalClassName = @"MBXAccounts";
static NSString * const MGLAccountManagerExternalMethodName = @"skuToken";
#endif
@interface MGLAccountManager ()
@@ -18,9 +19,9 @@ static const NSTimeInterval MGLAccountManagerSKUTokenLifespan = 3600;
@property (nonatomic) NSURL *apiBaseURL;
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
@property (atomic) NSString *skuToken;
@property (atomic) NSDate *skuTokenExpiration;
@property BOOL useExternalAccountManager;
#endif
@end
@implementation MGLAccountManager
@@ -42,7 +43,12 @@ static const NSTimeInterval MGLAccountManagerSKUTokenLifespan = 3600;
}
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
self.skuToken = [MBXSKUToken tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser];
// Check if we should use an external accounts library (e.g., provided by navigation)
id externalAccountsClass = NSClassFromString(MGLAccountManagerExternalClassName);
SEL externalSKUTokenMethod = NSSelectorFromString(MGLAccountManagerExternalMethodName);
if (externalAccountsClass != nil && [externalAccountsClass respondsToSelector:externalSKUTokenMethod]) {
MGLAccountManager.sharedManager.useExternalAccountManager = YES;
}
#endif
}
@@ -102,20 +108,14 @@ static const NSTimeInterval MGLAccountManagerSKUTokenLifespan = 3600;
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
+ (void)setSkuToken:(NSString *)skuToken {
MGLAccountManager.sharedManager.skuTokenExpiration = [NSDate dateWithTimeIntervalSinceNow:MGLAccountManagerSKUTokenLifespan];
MGLAccountManager.sharedManager.skuToken = skuToken;
}
+ (NSString *)skuToken {
return [MGLAccountManager.sharedManager isSKUTokenExpired] ?
[MBXSKUToken tokenForSKUID:MBXAccountsSKUIDMaps type:MBXAccountsSKUTypeUser] :
MGLAccountManager.sharedManager.skuToken;
}
- (BOOL)isSKUTokenExpired {
NSTimeInterval secondsUntilExpiration = [MGLAccountManager.sharedManager.skuTokenExpiration timeIntervalSinceNow];
return secondsUntilExpiration < 0;
if (MGLAccountManager.sharedManager.useExternalAccountManager) {
id externalAccountsClass = NSClassFromString(MGLAccountManagerExternalClassName);
SEL externalSKUTokenMethod = NSSelectorFromString(MGLAccountManagerExternalMethodName);
return ((NSString *(*)(id, SEL))[externalAccountsClass methodForSelector:externalSKUTokenMethod])(externalAccountsClass, externalSKUTokenMethod);
} else {
return MBXSKUToken.skuToken;
}
}
#endif

View File

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

View File

@@ -3,25 +3,33 @@
NS_ASSUME_NONNULL_BEGIN
typedef NSString *MBXAccountsSKUID NS_TYPED_ENUM;
FOUNDATION_EXTERN const MBXAccountsSKUID MBXAccountsSKUIDMaps;
FOUNDATION_EXTERN const MBXAccountsSKUID MBXAccountsSKUIDNavigation;
FOUNDATION_EXTERN const MBXAccountsSKUID MBXAccountsSKUIDMapsUser;
FOUNDATION_EXTERN const MBXAccountsSKUID MBXAccountsSKUIDNavigationUser;
FOUNDATION_EXTERN const MBXAccountsSKUID MBXAccountsSKUIDNavigationSession;
typedef NS_ENUM(NSInteger, MBXAccountsSKUType) {
MBXAccountsSKUTypeUser,
MBXAccountsSKUTypeSession
};
@interface MBXSKUToken : NSObject
/**
Generates a token for the given identifier and type.
Activates a specific SKU identifier.
@param skuId The sku identifier, e.g. maps or navigation.
@param type The type of token, e.g. user or session.
Defaults to `MBXAccountsSKUIDMapsUser`.
@return A SKU token for use with API requests.
See https://www.mapbox.com/pricing/ for more information
*/
+ (NSString *)tokenForSKUID:(MBXAccountsSKUID)skuId type:(MBXAccountsSKUType)type NS_SWIFT_NAME(token(for:type:));
+ (void)activateSKUID:(MBXAccountsSKUID)skuId;
/**
Resets the current session. Calling this method has no effect unless the type
of the SKU id is of kind session.
*/
+ (void)resetSession;
/**
Returns the current SKU token. `+[MBXSKUToken activateSKUID:]` must be called
before accessing this property unless an `MBXAccountsSKUIDMapsUser` token is preferred.
*/
@property (class, nonatomic, readonly) NSString *skuToken;
@end