Apple 应用内购买

Posted

技术标签:

【中文标题】Apple 应用内购买【英文标题】:Apple In-App Purchase 【发布时间】:2011-05-03 16:41:19 【问题描述】:

在设备上的沙盒环境中测试应用内购买时,我记录了以下错误:

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

【问题讨论】:

您没有互联网连接? @richard 我正在获取我在 iTunes Connect 上为我的应用程序注册的所有产品的产品信息。这意味着我的 Wi-Fi 连接处于活动状态 很抱歉再次提出一个老问题,但我现在遇到了完全相同的问题,您能找到除硬重置以外的任何其他解决方案吗? 对于它的价值,我遇到了完全相同的问题。我发现至少对我而言,原因是我的设备使用真实的苹果帐户而不是测试帐户登录。我注销了,然后一切正常。 【参考方案1】:

这可能很荒谬,但是 - 如果您确定您的代码是正确的 - 对您的 iPod 进行硬重置(完全擦除)(设置 => 常规 => 重置 => 删除所有内容和设置)。

【讨论】:

沙盒服务器是否有可能出现故障?你不认为我在这里附上的代码是正确的【参考方案2】:

如果您使用其他 iTunes 帐户登录,也会发生这种情况。要测试沙盒中的应用内购买,您需要从设置中的任何其他帐户注销。然后启动您的应用程序并在应用程序购买中执行。当系统询问您的帐户时,输入您创建为 iTunes 测试帐户的帐户。这样,您的沙盒环境就可以完美运行。希望这会有所帮助。

【讨论】:

【参考方案3】:

也许沙盒服务器已关闭。

我能够获取产品信息,但在请求购买时出现同样的错误

我查看了 Apple 开发者论坛,发现有更多人遇到同样的问题。 https://devforums.apple.com/index.jspa

我希望这可以为某人节省一些时间,因为我已经花了 4 个小时来解决这个问题。

【讨论】:

以上是关于Apple 应用内购买的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Apple 服务器获取应用内购买列表

一次购买的 Apple 应用内购买 transaction_id 有时会发生变化

如何通过Apple TestFlight测试应用内购买?

使用自动续订订阅的应用是不是需要 Apple 应用内购买?

iOS 11.1.2 中的 Apple 应用内购买错误

使用真实 Apple ID 的应用内购买行为