Apple In app purchase StoreKit error

2019-03-31 04:03发布

问题:

I am implementing in app purchase feature for a bookshelf but an error message RANDOMLY appear during purchasing books.

The error message is "Payment requests are restricted to products returned as valid via Store Kit's didReceiveResponse method."

I find the document in apple http://developer.apple.com/library/ios/#qa/qa2010/qa1691.html but it does not help to solve the problem...

in the same time observer print out another error : "Can't connect to the iTunes Store".

my in app purchase logic flow:

APP START:

- (void) requestProductDataWithSet:(NSSet*)_set
{
 SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: _set];
 request.delegate = self;
 [request start];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    //setup UI here
}

PURCHASE:

- (void) purchase:(SKProduct *)product
{
 if (!product || !verified) {
  return;
 }

 SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:product.productIdentifier]];
 request.delegate = self;
 [request start];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
if ([SKPaymentQueue canMakePayments] && [response.products count] > 0)
{
    NSLogInfo(@"xxxxxxxxx Make payment xxxxxxxxx");
    SKPayment *payment = [SKPayment paymentWithProduct:[response.products objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];   
  }
  else
  {
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase" message:@"You are not authorized to purchase from AppStore"
                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
   [alert show];
   [alert release];
  }
    }

Observer:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
 for (SKPaymentTransaction *transaction in transactions)
 {
  NSLogInfo(@"updatedTransactions transactionState:%d, productIdentifier:%@",transaction.transactionState,transaction.payment.productIdentifier);
  switch (transaction.transactionState)
  {
   case SKPaymentTransactionStatePurchased:

                [self completeTransaction:transaction];

                break;

            case SKPaymentTransactionStateFailed:

                [self failedTransaction:transaction];

                break;

            case SKPaymentTransactionStateRestored:

                [self restoreTransaction:transaction];

            default:

                break;
  }   
 }
}

回答1:

It looks like the problem isn't on your side. According to Technical Q&A QA1691, your code is fine.



回答2:

Try changing:

SKPayment *payment = [SKPayment paymentWithProduct:[response.products objectAtIndex:0]];

to:

SKPayment *payment = [SKPayment paymentWithProductIdentifier:[response.products objectAtIndex:0].productIdentifier];

I had the same problem as you and this change worked for me.