forked from kgn/BBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSKProductsRequest+BBlock.m
51 lines (43 loc) · 1.63 KB
/
SKProductsRequest+BBlock.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
//
// SKProductsRequest+BBlock.m
// BBlock
//
// Created by David Keegan on 8/7/12.
// Copyright (c) 2012 David Keegan. All rights reserved.
//
#import "SKProductsRequest+BBlock.h"
#import <objc/runtime.h>
static char SKProductsRequestDelegateBBlockKey;
@interface SKProductsRequestBBlockDelegate : NSObject
<SKProductsRequestDelegate>
@property (strong, nonatomic) SKProductsRequestBBlock block;
@end
@implementation SKProductsRequestBBlockDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
if(self.block){
self.block(response, nil);
}
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
if(self.block){
self.block(nil, error);
}
}
@end
@implementation SKProductsRequest(BBlock)
+ (instancetype)requestWithProductIdentifiers:(NSSet *)productIdentifiers andBlock:(SKProductsRequestBBlock)block{
return [[[self class] alloc] initWithProductIdentifiers:productIdentifiers andBlock:block];
}
- (instancetype)initWithProductIdentifiers:(NSSet *)productIdentifiers andBlock:(SKProductsRequestBBlock)block{
NSParameterAssert(block != nil);
if((self = [self initWithProductIdentifiers:productIdentifiers])){
SKProductsRequestBBlockDelegate *productsRequestDelegate = [[SKProductsRequestBBlockDelegate alloc] init];
objc_setAssociatedObject(self, &SKProductsRequestDelegateBBlockKey,
productsRequestDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.delegate = productsRequestDelegate;
productsRequestDelegate.block = block;
[self start];
}
return self;
}
@end