23

我有一些用户报告说,在尝试进行应用内购买后,该应用现在在启动时崩溃。我已要求他们删除并重新安装不起作用的应用程序,并试图要求他们进入飞行模式以停止任何不起作用的网络通信。

无论如何,我都无法在我的设备上复制该错误,并且我的应用内购买在沙盒和生产模式下都可以正常进行。我的想法是,他们的交易以某种方式收到了导致启动崩溃的 nil productIdentifier,但我不确定在应用程序启动时调用了哪些交易观察器方法,我可以为他们解决问题。

是否有办法“清除”事务队列或在启动时测试 nil productidentifiers 并允许这些用户至少让应用程序再次运行?我已经使用下面的代码完成了数百次应用购买,而这只是最近才开始发生的。应用启动时会调用哪些辅助方法?

在 AppDelegate.m

[[SKPaymentQueue defaultQueue] addTransactionObserver:[MovieClockIAPHelper sharedHelper]];

在应用程序帮助代码中:

- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {
    if ((self = [super init])) {

        // Store product identifiers
        _productIdentifiers = [productIdentifiers retain];

        // Check for previously purchased products

        NSMutableSet * purchasedProducts = [NSMutableSet set];
        for (NSString * productIdentifier in _productIdentifiers) {

            BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];

            if (productPurchased) {
                [purchasedProducts addObject:productIdentifier];
                NSLog(@"Previously purchased: %@", productIdentifier);
            }
            else{
            NSLog(@"Not purchased: %@", productIdentifier);
            }
        }
        self.purchasedProducts = purchasedProducts;

    }
    return self;
}

- (void)requestProducts {

    self.request = [[[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers] autorelease];
    _request.delegate = self;
    [_request start];

}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

    NSLog(@"Received products results...");   
    self.products = response.products;
    self.request = nil;    

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:_products];    
}


- (void)restoreCompletedTransactions {
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void)provideContent:(NSString *)productIdentifier {

    NSLog(@"Toggling flag for: %@", productIdentifier);
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [_purchasedProducts addObject:productIdentifier];

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier];

}

- (void)completeTransaction:(SKPaymentTransaction *)transaction {

    NSLog(@"completeTransaction...");

    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {

    NSLog(@"restoreTransaction...");


    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {

    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction];

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"in the payment queue");

    for (SKPaymentTransaction *transaction in transactions)
    {

        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
        }

}

-(void)buyProduct:(SKProduct *)product
{
    NSLog(@"In buyproduct Buying %@...", product.productIdentifier);

    SKPayment *payment = [SKPayment paymentWithProduct:product];

    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void)dealloc
{
    [_productIdentifiers release];
    _productIdentifiers = nil;
    [_products release];
    _products = nil;
    [_purchasedProducts release];
    _purchasedProducts = nil;
    [_request release];
    _request = nil;
    [super dealloc];
}

@end
4

5 回答 5

8

当交易在 SKPaymentTransactionStateRestored 中时,我遇到了类似的问题。我的测试表明这可能是 IOS 7.0.3 的问题,但我无法验证这一点。

StoreKit 保留了应用程序必须完成的事务的持久列表。正如您所注意到的,事务将在每次启动时报告,直到完成。

我们实施的解决方案是在使用之前从入口点检查产品标识符是否为 nil:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

即使我们收到一个带有零产品标识符的交易,我们也能够成功调用finishTransaction.

我希望这有帮助。

于 2013-11-07T21:02:25.910 回答
5

我的一个用户四天前就同样的问题抱怨过,并且能够向我发送崩溃日志。他在 iPhone 5,2 上使用 iOS 7.0.3。在尝试使用 originalTransaction.payment 中的 productIdentifier 属性构建字典时识别 SKPaymentTransactionStateRestored 后发生崩溃:

NSDictionary* userInfoDict = @{@"productID":transaction.payment.productIdentifier};

我认为 originalTransaction 或其属性 payment 或 productIdentifier 都为零。我从 transaction.payment.productIdentifier 中存储了 productIdentifier,而不是从 transaction.originalTransaction.payment.productIdentifier 中存储,同时从那时起恢复并将其用作 productIdentifier:

case SKPaymentTransactionStateRestored:
{
  NSString *productID = transaction.originalTransaction.payment.productIdentifier;
  if (!productID) {
    productID = transaction.payment.productIdentifier;
  }
  [self handlePurchaseSuccessful:transaction.originalTransaction productIdentifier:productID];
}

如果解决了问题,仍在等待审查/反馈。

于 2013-11-12T20:08:26.400 回答
3

详细说明肖恩的答案,- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions您可能有一些这样的代码:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:

                                [self completeTransaction:transaction];

                                break;

                        case SKPaymentTransactionStateFailed:

                                [self failedTransaction:transaction];

                                break;

                        case SKPaymentTransactionStateRestored:

                                [self restoreTransaction:transaction];

                        default:

                                break;
        }     
    }
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{   
    /* Handle restore here */
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
}

您可以restoreTransaction:通过添加检查以查看 productIdentifier 是否为 nil 来处理 nil productIdentifiers,如下所示:

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{   
    if (!transaction.originalTransaction.payment.productIdentifier) {
        NSLog(@"productIdentifier is nil; Apple bug?");
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
        return;
    }

    /* Handle restore here */
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

这种技术在我的应用程序中为我解决了这个问题。应用程序启动,记录“productIdentifier is nil; Apple bug?” 并没有崩溃。然后,当我手动重新恢复交易时,Apple 发送了一个有效交易,并且该应用程序按设计运行。

于 2013-11-17T05:14:47.870 回答
3

我在上面发布的 Marimba修复了这个问题,它对我们的客户有帮助:

case SKPaymentTransactionStateRestored:
{
  NSString *productID = transaction.originalTransaction.payment.productIdentifier;
  if (productID == nil) {
    productID = transaction.payment.productIdentifier;
  }
  [self handlePurchaseSuccessful:transaction.originalTransaction productIdentifier:productID];
}
于 2013-12-02T10:55:32.810 回答
0

我遇到了同样的问题paymentQueue:updatedTransactions:,在.SKPaymentTransactionSKPaymentTransactionStateRestorednil productIdentifieroriginalTransaction

这听起来很奇怪,可能是 7.0.3 SDK 的错误。

你们都还在用 SDK 6 编译吗?

我正要重新提交我的应用程序到商店,看看问题是否得到解决,但只是完成此类交易,但我希望现在可以处理具有有效产品标识符的其他交易,因为应用程序不会崩溃,所以我知道什么恢复。

于 2013-11-13T18:07:41.377 回答