-(BOOL)putStringToItunes:(NSData*)iapData{//用戶購成功的transactionReceiptjson
// base64EncodedStringapp
NSString*encodingStr = [iapData base64Encoding];url
NSString *URL=@"https://sandbox.itunes.apple.com/verifyReceipt";code
//https://buy.itunes.apple.com/verifyReceiptorm
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];// autorelease];ip
[request setURL:[NSURL URLWithString:URL]];get
[request setHTTPMethod:@"POST"];string
//設置contentTypeit
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];io
//設置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;
}
這個驗證不建議客戶端來作,應該由服務端來作。
可是我這邊狀況是服務端驗證總是不成功,爲了證實我傳給他的憑證沒有問題,找來了這個方法,證實我傳的憑證沒有問題。