我已经在 Sandbox 环境中测试了 Consumable 类型的 IAP。我想确认我购买的商品是否有效。但结果总是返回状态“21004”。我对分享秘密什么都不做。所以你可以看一下我在成功购买商品时跟随苹果的示例代码:
- (void)verifyStatus:(SKPaymentTransaction *)transaction {
NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSError *receiptError;
BOOL isPresent = [[[NSBundle mainBundle] appStoreReceiptURL] checkResourceIsReachableAndReturnError:&receiptError];
if (!isPresent) {
NSLog(@"Validation failed");
}
NSString *receiptStr = [receiptData base64EncodedStringWithOptions:0];
NSDictionary *requestContents = @{@"receipt-data":receiptStr};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:nil];
if (!requestData) { /* ... Handle error ... */ }
NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"Respond : %@",jsonResponse);
if (!jsonResponse) { /* ... Handle error ...*/ }
/* ... Send a response back to the device ... */
}
}];
}
我遇到了什么问题?请分享你的经验。谢谢