苹果应用内购买

7
在设备上测试SANDBOX环境中的应用内购买时,我记录了以下错误: Error Domain=SKErrorDomain Code=0 "Cannot connect to iTunes Store" UserInfo=0x2916a0 {NSLocalizedDescription=Cannot connect to iTunes Store}. 我能够检索通过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

3
您没有互联网连接吗? - Richard H
@richard 我正在获取我在iTunes Connect注册的所有应用程序的产品信息。这意味着我的Wi-Fi连接是活动的。 - Pranav Jaiswal
抱歉重新提出一个旧问题,但我现在遇到了完全相同的问题,除了硬重置之外,您能找到其他解决方案吗? - Can Poyrazoğlu
1
说句实话,我也遇到了完全相同的问题。我发现,至少对我来说,原因是我的设备登录了真实的苹果账户而不是测试账户。我注销了账户,然后一切都正常工作了。 - John Gaby
3个回答

3
这可能听起来很荒谬,但如果你确定你的代码是正确的,那就对你的iPod进行硬重置(完全擦除)(设置=>通用=>重置=>清除所有内容和设置)。

沙盒服务器有可能宕机了吗?你不认为我在这里附加的代码是正确的吗? - Pranav Jaiswal

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

0

可能是沙盒服务器出了问题。

我可以获取产品信息,但在请求购买时会出现同样的错误。

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

希望这能为某些人节省时间,因为我已经花了4个小时在这上面了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接