iap验证。

-(BOOL)putStringToItunes:(NSData*)iapData{//用户购成功的transactionReceipt

//    base64EncodedString

    NSString*encodingStr = [iapData base64Encoding];

    

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

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

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

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

    [request setHTTPMethod:@"POST"];

    //设置contentType

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

    //设置Content-Length

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

    

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

//    SBJsonWriter *writer = [SBJsonWriter new];

    [request setHTTPBody:[[body JSONRepresentation] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

//    [request setHTTPBody:[[writer stringWithObject:body] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion: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];

    DebugLog(@"-results-  %@",results);

    NSDictionary*dic = [results JSONValue];

    if([[dic objectForKey:@"status"] intValue]==0){//注意,status=@"0" 是验证收据成功

        return true;

    }

    return false;

}

 

 

这个验证不建议客户端来做,应该由服务端来做。

但是我这边情况是服务端验证老是不成功,为了证明我传给他的凭证没有问题,找来了这个方法,证明我传的凭证没有问题。

posted on 2014-12-08 14:20  三十一  阅读(322)  评论(0编辑  收藏  举报

导航