在应用程序购买 - 设备崩溃

时间:2013-07-23 20:18:49

标签: ios in-app-purchase

如果我在模拟器中运行我的应用程序,则表示failedTransaction ...

如果我在我的iPhone上运行它会崩溃并出现此错误:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [__ NSSetM addObject:]:object不能为nil'

这里:是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

SKProduct * product = (SKProduct *) _products[indexPath.row];
cell.textLabel.text = product.localizedTitle;
[_priceFormatter setLocale:product.priceLocale];
cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price];


if([[ScoreboardIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.accessoryView = nil;
} else {
    UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyButton.frame = CGRectMake(0, 0, 72, 37);
    [buyButton setTitle:@"Buy" forState:UIControlStateNormal];
    buyButton.tag = indexPath.row;
    [buyButton addTarget:self action:@selector(buyButtonTapped:)  forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = buyButton;
}

return cell;
}

- (void)buyButtonTapped:(id)sender {

UIButton *buyButton = (UIButton *)sender;
SKProduct *product = _products[buyButton.tag];

NSLog(@"Buying %@...", product.productIdentifier);
[[ScoreboardIAPHelper sharedInstance] buyProduct:product];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];



}

我在这里得到了错误:

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

[_purchasedProductIdentifiers addObject:productIdentifier];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier];

 }

2 个答案:

答案 0 :(得分:4)

[__NSSetM addObject:]: object cannot be nil

这意味着你有一个可变的集合,你正在调用addObject:,但你传入的变量是nil

在您发布的代码中,您调用此代码的唯一位置是:

[_purchasedProductIdentifiers addObject:productIdentifier];

...所以无论你打电话给provideContentForProductIdentifier:,都要传递nil

答案 1 :(得分:1)

更改_purchasedProductIdentifiers

的分配

替换_purchasedProductIdentifiers = [NSMutableSet set];

_purchasedProductIdentifiers = [[NSMutableSet alloc] init];