Skip to content

Commit

Permalink
Merge pull request #27 from crossfield/fix-dateformatter-ios
Browse files Browse the repository at this point in the history
Fix crash on iOS < 10 as NSISO8601DateFormatter is unavailable there
  • Loading branch information
jeiting authored Oct 25, 2018
2 parents 3fa127c + e039423 commit c386035
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ios/RCPurchaserInfo+RNPurchases.m
Original file line number Diff line number Diff line change
@@ -8,25 +8,40 @@

#import "RCPurchaserInfo+RNPurchases.h"

static NSDateFormatter *formatter;
static dispatch_once_t onceToken;

static NSString * stringFromDate(NSDate *date)
{
dispatch_once(&onceToken, ^{
// Here we're not using NSISO8601DateFormatter as we need to support iOS < 10
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter = dateFormatter;
});

return [formatter stringFromDate:date];
}

@implementation RCPurchaserInfo (RNPurchases)

- (NSDictionary *)dictionary
{
NSISO8601DateFormatter *formatter = [NSISO8601DateFormatter new];

NSMutableDictionary *allExpirations = [NSMutableDictionary new];
for (NSString *productIdentifier in self.allPurchasedProductIdentifiers) {
NSDate *date = [self expirationDateForProductIdentifier:productIdentifier];
allExpirations[productIdentifier] = date ? [formatter stringFromDate:date] : [NSNull null];
allExpirations[productIdentifier] = stringFromDate(date) ?: [NSNull null];
}

NSMutableDictionary *expirationsForActiveEntitlements = [NSMutableDictionary new];
for (NSString *entId in self.activeEntitlements) {
NSDate *date = [self expirationDateForEntitlement:entId];
expirationsForActiveEntitlements[entId] = date ? [formatter stringFromDate:date] : [NSNull null];;
expirationsForActiveEntitlements[entId] = stringFromDate(date) ?: [NSNull null];;
}

id latestExpiration = self.latestExpirationDate ? [formatter stringFromDate:self.latestExpirationDate] : [NSNull null];
id latestExpiration = stringFromDate(self.latestExpirationDate) ?: [NSNull null];

return @{
@"activeEntitlements": self.activeEntitlements.allObjects,

0 comments on commit c386035

Please sign in to comment.