スキップしてメイン コンテンツに移動

receipt

    [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];

}

コメント

このブログの人気の投稿

日本に来た 今三週間

四月六日に日本に来た、今三週間になった。 嬉しくなかった、悲しくなかった。 日本の携帯電話のカメラはとてもいいよ、私は今撮影が好きだ、いつも寮に帰る途中で花とか魚とか写真を撮る。これも生活の趣味だ。 滴は綺麗だ 桜が凋んで落ちる 天気がいいとき、自転車に乗って、食材を買った。 自分で料理をつくる、おいしそうでしょう。 昨日清水寺に行った、天気はとても熱いね。日に焼けすぎたので、腕は今赤い。 側に花があるので、赤い清水寺はもっと美しそうだ。 木の色は艶やかだ。