在应用程序购买之后,应用程序在启动时崩溃。产品识别=无?

时间:2013-11-06 16:15:29

标签: ios in-app-purchase null storekit

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

我无法在我的设备上复制错误,而且我的应用内购买在沙箱和制作模式中都很好。我的想法是,他们的事务以某种方式收到了一个nil productIdentifier导致启动崩溃,但我不确定在app启动时调用哪些事务观察器方法我可以为他们解决问题。

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

在AppDelegate.m中

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

在app帮助程序代码中:

- (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

5 个答案:

答案 0 :(得分:8)

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

StoreKit会保留应用程序必须完成的持久事务列表。正如您所知,交易将在每次启动时报告,直至完成。

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

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

即使我们收到了包含零产品标识符的交易,我们也能成功拨打finishTransaction

我希望这会有所帮助。

答案 1 :(得分:5)

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

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

我认为originalTransaction或其属性payment或productIdentifier都是nil。我从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];
}

如果可以解决问题,仍在等待审核/反馈。

答案 2 :(得分:3)

在Shawn的答案中阐述,在- (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:中的nil productIdentifier,以查看productIdentifier是否为nil,如下所示:

- (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为零;苹果虫?"并没有崩溃。当我然后手动重新恢复交易时,Apple发送了一个有效的交易,该应用程序按设计工作。

答案 3 :(得分:3)

我解决了这个问题as Marimba posted above,这对我们的客户有所帮助:

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

答案 4 :(得分:0)

我遇到同样的问题paymentQueue:updatedTransactions:正在调用SKPaymentTransaction个实例,其中SKPaymentTransactionStateRestorednil productIdentifier位于originalTransaction }。

这听起来很奇怪,可能是7.0.3 SDK bug。

你们还在使用SDK 6进行编译吗?

我即将重新提交我的应用程序到商店,看看问题是否得到解决,但只是完成了这样的交易,但我希望现在处理具有有效productIdentifier的其他交易,因为应用程序不会崩溃,所以我知道要恢复什么。