AppStore IAP 客户端校验代码

-(BOOL)putStringToItunes:(SKPaymentTransaction*)transaction

{

    NSData * iapData = transaction.transactionReceipt;

    NSString*encodingStr = [iapData base64EncodedString];

    

    //    NSString *URL=@"https://sandbox.itunes.apple.com/verifyReceipt";

    NSString *URL=@"https://buy.itunes.apple.com/verifyReceipt";

    

    //https://buy.itunes.apple.com/verifyReceipt

    NSMutableURLRequest *request = [[NSMutableURLRequestalloc] init];// autorelease];

    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];

    //设置contentType

    [request addValue:@"application/json"forHTTPHeaderField:@"Content-Type"];

    //设置Content-Length

    [request setValue:[NSStringstringWithFormat:@"%d", [encodingStr length]] forHTTPHeaderField:@"Content-Length"];

    

    NSDictionary* body = [NSDictionary dictionaryWithObjectsAndKeys:encodingStr, @"receipt-data", nil];

    SBJsonWriter *writer = [SBJsonWriternew];

    [request setHTTPBody:[[writer stringWithObject:body] dataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES]];

    NSHTTPURLResponse *urlResponse=nil;

    NSError *errorr=nil;

    NSData *receivedData = [NSURLConnection sendSynchronousRequest:request

                                                 returningResponse:&urlResponse

                                                             error:&errorr];

    

    //解析

    NSString *results=[[NSString alloc]initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding];

    NSLog(@"-验证结果-  %@",results);

    NSDictionary*dic = [results JSONValue];

    if([[dic objectForKey:@"status"] intValue]==0)

    {

        //注意,status=@"0" 是验证收据成功

        return true;

    }

    returnfalse;

}

posted @ 2013-10-10 09:33  haroel  阅读(610)  评论(0编辑  收藏  举报