苹果应用内购买
在设备上的沙盒环境中测试应用内购买时,我记录了以下错误:
Error Domain=SKErrorDomain Code=0“无法连接到 iTunes Store”UserInfo=0x2916a0 {NSLocalizedDescription=无法连接到 iTunes Store}.t
我能够检索我通过 iTunes Connect 注册的产品 ID。我在表格视图中显示与这些产品相关的数据以及购买选项。 当我尝试购买产品时,会启动交易,但它不会要求我提供任何测试用户详细信息和信息。我收到上面提到的错误。
我提供了我实现的代码。
//the below code is for RETREIVING THE PRODUCT id's
#pragma mark Store kit
-(IBAction)sendProductInfoRequest{
NSLog(@"sendProductInfoRequest");
NSSet *identifiersSet=[NSSet setWithObjects:[NSString stringWithFormat:@"%@",@".15April2011"],[NSString stringWithFormat:@"%@",@"15April201102"],nil];
SKProductsRequest *productRequest=[[SKProductsRequest alloc] initWithProductIdentifiers:identifiersSet];
productRequest.delegate=self;
[productRequest start];
NSLog(@"completing sendProductInfoRequest");
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"yoooo!");
NSLog(@"The product request didReceiveResponse :%@",[response description]);
NSLog(@"The products are :%@",[response.products description]);
NSLog(@"The invalidProductIdentifiers are:%@",[response.invalidProductIdentifiers description]);
NSArray *products=response.products;
for(SKProduct *currentProduct in products){
NSLog(@"THE Product price is :%@",currentProduct.price);
NSLog(@"THE Product description is :%@",currentProduct.localizedDescription);
NSLog(@"THE Product title is :%@",currentProduct.localizedTitle);
NSLog(@"THE Product's product identifier is :%@",currentProduct.productIdentifier);
}
}
/the BuyProducts method is called when user clicks buy button related to a particular product
-(IBAction)BuyProducts
{
if ([SKPaymentQueue canMakePayments])
{
[self makePaymentRequestForThisProduct:isbnText.text];
}
}
-(void)makePaymentRequestForThisProduct:(NSString*)productID
{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productID];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
/I have added the transaction observer in applicationDidFinishLaunching method of AppDelegate
//Transaction Observer is a class
TransactionObserver *observer;
observer = [[TransactionObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
//the TransactionObserver.m class
@implementation TransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"failed transaction");
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"successful purchase");
// [self recordTransaction: transaction];
//[self provideContent: transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"restored incomplete transaction");
// [self recordTransaction: transaction];
// [self provideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"The error description is:%@",[transaction.error description]);
if (transaction.error.code != SKErrorPaymentCancelled)
{
if(transaction.error.code == SKErrorUnknown) {
NSLog(@"Unknown Error (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorClientInvalid) {
NSLog(@"Client invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorPaymentInvalid) {
NSLog(@"Payment invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorPaymentNotAllowed) {
NSLog(@"Payment not allowed (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
@end
While testing In-App-Purchase in the SANDBOX ENVIRONMENT on the device, I'm logging the following error :
Error Domain=SKErrorDomain Code=0 "Cannot connect to iTunes Store" UserInfo=0x2916a0 {NSLocalizedDescription=Cannot connect to iTunes Store}.t
I'm able to retreive the product id's which i registered through iTunes Connect. I'm displaying data related with these products as in a table view along with Buying option.
When i try to buy a product a transaction is initiated but it doesn't ask me for any test user details & i get the error as mentioned above.
I'm providing the code which I implemented.
//the below code is for RETREIVING THE PRODUCT id's
#pragma mark Store kit
-(IBAction)sendProductInfoRequest{
NSLog(@"sendProductInfoRequest");
NSSet *identifiersSet=[NSSet setWithObjects:[NSString stringWithFormat:@"%@",@".15April2011"],[NSString stringWithFormat:@"%@",@"15April201102"],nil];
SKProductsRequest *productRequest=[[SKProductsRequest alloc] initWithProductIdentifiers:identifiersSet];
productRequest.delegate=self;
[productRequest start];
NSLog(@"completing sendProductInfoRequest");
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"yoooo!");
NSLog(@"The product request didReceiveResponse :%@",[response description]);
NSLog(@"The products are :%@",[response.products description]);
NSLog(@"The invalidProductIdentifiers are:%@",[response.invalidProductIdentifiers description]);
NSArray *products=response.products;
for(SKProduct *currentProduct in products){
NSLog(@"THE Product price is :%@",currentProduct.price);
NSLog(@"THE Product description is :%@",currentProduct.localizedDescription);
NSLog(@"THE Product title is :%@",currentProduct.localizedTitle);
NSLog(@"THE Product's product identifier is :%@",currentProduct.productIdentifier);
}
}
/the BuyProducts method is called when user clicks buy button related to a particular product
-(IBAction)BuyProducts
{
if ([SKPaymentQueue canMakePayments])
{
[self makePaymentRequestForThisProduct:isbnText.text];
}
}
-(void)makePaymentRequestForThisProduct:(NSString*)productID
{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productID];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
/I have added the transaction observer in applicationDidFinishLaunching method of AppDelegate
//Transaction Observer is a class
TransactionObserver *observer;
observer = [[TransactionObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
//the TransactionObserver.m class
@implementation TransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"failed transaction");
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"successful purchase");
// [self recordTransaction: transaction];
//[self provideContent: transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"restored incomplete transaction");
// [self recordTransaction: transaction];
// [self provideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"The error description is:%@",[transaction.error description]);
if (transaction.error.code != SKErrorPaymentCancelled)
{
if(transaction.error.code == SKErrorUnknown) {
NSLog(@"Unknown Error (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorClientInvalid) {
NSLog(@"Client invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorPaymentInvalid) {
NSLog(@"Payment invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
if(transaction.error.code == SKErrorPaymentNotAllowed) {
NSLog(@"Payment not allowed (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
message: @"There was an error purchasing this item please try again."
delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[failureAlert show];
[failureAlert release];
}
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能很荒谬,但是 - 如果您确定您的代码是正确的 - 对您的 iPod 进行硬重置(完全擦除)(设置 => 常规 => 重置 => 删除所有内容和设置)。
This may be ridiculous, but -- if you're certain your code is correct -- do a hard reset (full wipe) of your iPod (Settings => General => Reset => Erase All Content and Settings).
如果您使用其他 iTunes 帐户登录,也会发生这种情况。要在沙盒中测试应用内购买,您需要从“设置”中的任何其他帐户注销。然后启动您的应用程序并进行应用程序购买。当系统询问您的帐户时,请输入您创建的 iTunes 测试帐户。这样您的沙盒环境就能完美运行。希望这会有所帮助。
It also happens if you are logged in with some other iTunes Account. To test the In App purchase in Sandbox box you need to log out from any other account from Setting. Then launch your application and Do in App Purchase. When your account is asked enter the one you created as iTunes Test Account. This way your Sandbox environment would work perfectly. Hope this would be helpful.
也许沙盒服务器已关闭。
我能够获取产品信息,但在请求购买时遇到同样的错误,
我检查了 Apple 开发者论坛,并且更多人遇到了同样的问题。
https://devforums.apple.com/index.jspa
我希望这可以节省一些人的时间一天,因为我为此花了 4 个小时。
Maybe the Sandbox servers are down.
I am able to get product info but I get the same error when requesting a purchase
I checked the Apple Developer Forums and more people are having the same issue.
https://devforums.apple.com/index.jspa
I hope this saves someone some time some day, as I've spent 4 hours around this.