我已经在 Apple 门户中配置了我的 iOS 应用程序的应用内购买,我要求我的产品使用SKProductsRequest
并正确接收,然后将它们存储在NSMutableDictionary
.
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse*)response
{
NSLog(@"%@", @"AppStore: Accepted");
for (SKProduct * s in response.products) {
NSLog(@" [%@]", s.productIdentifier);
}
[productsById removeAllObjects];
for (SKProduct * product in response.products) {
[productsById setObject:product forKey:product.productIdentifier];
}
[request release];
}
但是,当尝试进行购买时,这addPayment
条线会抛出'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
-(void)purchase:(NSString *)productId {
SKProduct *selectedProduct = [productsById objectForKey:productId];
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct: selectedProduct];
payment.quantity = 1;
[[SKPaymentQueue defaultQueue] addTransactionObserver: self];
[[SKPaymentQueue defaultQueue] addPayment: payment]; //<- this line throws an exception
}
正如苹果的文档所说,我已经检查了 productId 是一个有效的产品,并且 payment.quantity 是 > 0。我在模拟器和 iPad 中都尝试过,结果相同。提前感谢您的帮助。