我知道这个问题已被多次询问,但我仍然遇到问题。我有一些非消耗品应用程序购买代码,如果用户留在现场并一次购买它们,则可以正常工作。但是,如果用户离开此菜单并返回该菜单,代码将在完成事务时崩溃。我正在使用cocos2d来实现它的价值。
来自 MKStoremanager.m
-(void) provideContent: (NSString*) productIdentifier
{
NSLog(@"productIdentifier=%@", productIdentifier);
nc = [NSNotificationCenter defaultCenter];
featureAPurchased = YES;
[nc postNotificationName:@"featureAPurchased" object:nil]; //<--crashes here
[MKStoreManager updatePurchases];
}
来自 MSStoreObverser.m
- (void) completeTransaction: (SKPaymentTransaction *)transaction { NSLog(@"completeTransaction");
NSLog(@"transaction.payment.productIdentifier=%@",transaction.payment.productIdentifier);
//[[NSNotificationCenter defaultCenter] removeObserver:self]; //<--tried removing observer. Still crashes
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
[[MKStoreManager sharedManager] provideContent: transaction.payment.productIdentifier];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"paymentQueue");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction]; //<--Breaks here
NSLog(@"Purchase was a success -J");
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}
}
我认为问题是NSNotificationCenter观察者没有被正确删除,但我不确定原因。
答案 0 :(得分:2)
正如我的评论建议的那样,我们需要删除观察者,以确保我们不会向解除分配的内容发送消息。
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}