forked from MKSG-MugunthKumar/MKStoreKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MKSKProduct.m
304 lines (245 loc) · 9.64 KB
/
MKSKProduct.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// MKSKProduct.m
// MKStoreKitDemo
// Version 4.1
//
// Created by Mugunth on 04/07/11.
// Copyright 2011 Steinlogic. All rights reserved.
// Licensing (Zlib)
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// As a side note on using this code, you might consider giving some credit to me by
// 1) linking my website from your app's website
// 2) or crediting me inside the app's credits page
// 3) or a tweet mentioning @mugunthkumar
// 4) A paypal donation to [email protected]
#import "MKSKProduct.h"
static void (^onReviewRequestVerificationSucceeded)();
static void (^onReviewRequestVerificationFailed)();
static NSURLConnection *sConnection;
static NSMutableData *sDataFromConnection;
@implementation MKSKProduct
@synthesize onReceiptVerificationFailed;
@synthesize onReceiptVerificationSucceeded;
@synthesize receipt;
@synthesize productId;
@synthesize theConnection;
@synthesize dataFromConnection;
+(NSString*) deviceId {
#if TARGET_OS_IPHONE
UIDevice *dev = [UIDevice currentDevice];
NSString *uniqueID;
if ([dev respondsToSelector:@selector(uniqueIdentifier)])
uniqueID = [dev valueForKey:@"uniqueIdentifier"];
else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:@"uniqueID"];
if (uuid)
uniqueID = (NSString *)uuid;
else {
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
uniqueID = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
[defaults setObject:uniqueID forKey:@"uniqueID"];
}
}
#elif TARGET_OS_MAC
kern_return_t kernResult;
mach_port_t master_port;
CFMutableDictionaryRef matchingDict;
io_iterator_t iterator;
io_object_t service;
CFDataRef macAddress = nil;
kernResult = IOMasterPort(MACH_PORT_NULL, &master_port);
if (kernResult != KERN_SUCCESS) {
printf("IOMasterPort returned %d\n", kernResult);
return nil;
}
matchingDict = IOBSDNameMatching(master_port, 0, "en0");
if(!matchingDict) {
printf("IOBSDNameMatching returned empty dictionary\n");
return nil;
}
kernResult = IOServiceGetMatchingServices(master_port, matchingDict, &iterator);
if (kernResult != KERN_SUCCESS) {
printf("IOServiceGetMatchingServices returned %d\n", kernResult);
return nil;
}
while((service = IOIteratorNext(iterator)) != 0)
{
io_object_t parentService;
kernResult = IORegistryEntryGetParentEntry(service, kIOServicePlane, &parentService);
if(kernResult == KERN_SUCCESS)
{
if(macAddress)
CFRelease(macAddress);
macAddress = IORegistryEntryCreateCFProperty(parentService, CFSTR("IOMACAddress"), kCFAllocatorDefault, 0);
IOObjectRelease(parentService);
}
else {
printf("IORegistryEntryGetParentEntry returned %d\n", kernResult);
}
IOObjectRelease(service);
}
return [[NSString alloc] initWithData:(__bridge NSData*) macAddress encoding:NSASCIIStringEncoding];
#endif
}
-(id) initWithProductId:(NSString*) aProductId receiptData:(NSData*) aReceipt
{
if((self = [super init]))
{
self.productId = aProductId;
self.receipt = aReceipt;
}
return self;
}
#pragma mark -
#pragma mark In-App purchases promo codes support
// This function is only used if you want to enable in-app purchases for free for reviewers
// Read my blog post http://mk.sg/31
+(void) verifyProductForReviewAccess:(NSString*) productId
onComplete:(void (^)(NSNumber*)) completionBlock
onError:(void (^)(NSError*)) errorBlock
{
if(REVIEW_ALLOWED)
{
onReviewRequestVerificationSucceeded = [completionBlock copy];
onReviewRequestVerificationFailed = [errorBlock copy];
NSString *uniqueID = [self deviceId];
// check udid and featureid with developer's server
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", OWN_SERVER, @"featureCheck.php"]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *postData = [NSString stringWithFormat:@"productid=%@&udid=%@", productId, uniqueID];
NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
sConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
[sConnection start];
}
else
{
completionBlock([NSNumber numberWithBool:NO]);
}
}
- (void) verifyReceiptOnComplete:(void (^)(void)) completionBlock
onError:(void (^)(NSError*)) errorBlock
{
self.onReceiptVerificationSucceeded = completionBlock;
self.onReceiptVerificationFailed = errorBlock;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", OWN_SERVER, @"verifyProduct.php"]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *receiptDataString = [[NSString alloc] initWithData:self.receipt
encoding:NSASCIIStringEncoding];
NSString *postData = [NSString stringWithFormat:@"receiptdata=%@", receiptDataString];
NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
self.theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
[self.theConnection start];
}
#pragma mark -
#pragma mark NSURLConnection delegate
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
self.dataFromConnection = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[self.dataFromConnection appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:self.dataFromConnection
encoding:NSASCIIStringEncoding];
self.dataFromConnection = nil;
if([responseString isEqualToString:@"YES"])
{
if(self.onReceiptVerificationSucceeded)
{
self.onReceiptVerificationSucceeded();
self.onReceiptVerificationSucceeded = nil;
}
}
else
{
if(self.onReceiptVerificationFailed)
{
self.onReceiptVerificationFailed(nil);
self.onReceiptVerificationFailed = nil;
}
}
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
self.dataFromConnection = nil;
if(self.onReceiptVerificationFailed)
{
self.onReceiptVerificationFailed(nil);
self.onReceiptVerificationFailed = nil;
}
}
+ (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
sDataFromConnection = [[NSMutableData alloc] init];
}
+ (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[sDataFromConnection appendData:data];
}
+ (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:sDataFromConnection
encoding:NSASCIIStringEncoding];
sDataFromConnection = nil;
if([responseString isEqualToString:@"YES"])
{
if(onReviewRequestVerificationSucceeded)
{
onReviewRequestVerificationSucceeded();
onReviewRequestVerificationFailed = nil;
}
}
else
{
if(onReviewRequestVerificationFailed)
onReviewRequestVerificationFailed(nil);
onReviewRequestVerificationFailed = nil;
}
}
+ (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
sDataFromConnection = nil;
if(onReviewRequestVerificationFailed)
{
onReviewRequestVerificationFailed(nil);
onReviewRequestVerificationFailed = nil;
}
}
@end