[self checkReceipt];
return YES;
}
//check whether purchased, if purchased, remove ads.
-(void)checkReceipt{
//Get the receipt URL
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
//Check if it exists
if ([[NSFileManager defaultManager] fileExistsAtPath:receiptURL.path]) {
//Encapsulate the base64 encoded receipt on NSData
NSData *receiptData = [NSData dataWithContentsOfFile:receiptURL.path];
NSString *base64Receipt = [self base64ForData:receiptData];
NSLog(@"base64Receipt >- %@", base64Receipt);
//Prepare the request to send "receipt-data:[the receipt]" via POST method as json object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"]];
[request setHTTPMethod:@"POST"];
NSDictionary *requestDic = [NSDictionary dictionaryWithObject:base64Receipt forKey:@"receipt-data"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestDic options:0 error:nil];
[request setHTTPBody:jsonData];
//Send the request
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
if (connectionError) {
return; //A connection error ocurred
}
//Get a NSDictionary from the json object
NSError *parseError;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parseError];
if (parseError) {
return; //Unable to parse the json object
}
//Get the purchased version by the user
float version = [[[responseDic objectForKey:@"receipt"] objectForKey:@"original_application_version"] floatValue];
float info = [[[responseDic objectForKey:@"receipt"] objectForKey:@"latest_receipt_info"] floatValue];
NSLog(@"status >- %@", [responseDic objectForKey:@"status"]);
NSLog(@"all >- %@", [responseDic objectForKey:@"receipt"]);
NSLog(@"version >- %f", version);
NSLog(@"info >- %f", info);
}];
}
}
-(NSString*)base64ForData :(NSData *)theData{
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
コメント
コメントを投稿