-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIAPStoreManager.m
70 lines (54 loc) · 1.83 KB
/
IAPStoreManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import "IAPStoreManager.h"
#import "IAPCatalogue.h"
@interface IAPStoreManager()
@property (nonatomic, strong) IAPCatalogue* catalogue;
@end
@implementation IAPStoreManager
@synthesize catalogue = _catalogue;
static NSTimeInterval autoUpdateInterval = 30; // Check the catalogue for expiry every 30 seconds. Need to check this often in case the application was sent into the background.
static NSTimeInterval expiryInterval = 60 * 60 * 24; // 24 hours.
+ (IAPStoreManager *)sharedInstance
{
static IAPStoreManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[IAPStoreManager alloc] init];
});
return sharedInstance;
}
- (id)init {
self = [super init];
if (self) {
self.catalogue = [[IAPCatalogue alloc] init];
}
return self;
}
- (void)autoUpdate {
[NSTimer scheduledTimerWithTimeInterval:autoUpdateInterval target:self selector:@selector(checkCatalogueNeedsUpdating) userInfo:nil repeats:YES];
[self checkCatalogueNeedsUpdating];
}
- (void)checkCatalogueNeedsUpdating {
if ([self hasCatalogueExpired:self.catalogue]) {
[self.catalogue update:self];
}
}
- (BOOL)hasCatalogueExpired:(IAPCatalogue*)catalogue {
NSDate* lastUpdatedAt = catalogue.lastUpdatedAt;
if (!lastUpdatedAt) {
return YES;
}
NSDate* now = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval secondsSinceLastUpdate = [lastUpdatedAt timeIntervalSinceDate:now];
BOOL expired = secondsSinceLastUpdate > expiryInterval;
return expired;
}
- (IAPProduct*)productForIdentifier:(NSString*)identifier {
return [self.catalogue productForIdentifier:identifier];
}
- (void)restoreAllProducts {
[self.catalogue restoreAllProducts];
}
- (NSDictionary*)products {
return self.catalogue.products;
}
@end