0

这就是我所说的:

/**
 *  called when we are notified that a product has been purchased
 *
 *  @param  notification                the notification object calling this method
 */
- (void)productPurchased:(NSNotification *)notification
{
    //  the notification object is purchased product identifier
    NSString *productIdentifier         = notification.object;

    NSLog(@"Product purchased");

    //  reload appropriate cell with checkmark
    [_products enumerateObjectsUsingBlock:^(SKProduct *product, NSUInteger idx, BOOL *stop)
    {
        NSLog(@"Block executing with product: %@", product.productIdentifier);
        NSLog(@"Needs to match: %@", productIdentifier);

        if ([product.productIdentifier isEqualToString:productIdentifier])
        {
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]]
                                  withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"Reloading due to purchase");
            *stop                       = YES;
        }
    }];
}

但是,该块永远不会被调用,因此表格视图单元格永远不会被重新加载。我可以说该块永远不会执行,因为日志永远不会出现在控制台中。但是,通过放置断点,我已经能够确定 productPurchased: 方法肯定被调用了。

任何帮助表示赞赏。

谢谢你。

编辑:我现在更改了上面的代码,以允许在整个执行过程中进行更多的日志记录。从我通过查看控制台收集到的信息来看,传入的通知对象是表格视图本身:

<UITableView: 0x1eaffe00; frame = (0 0; 320 704); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1e57dfe0>; layer = <CALayer: 0x1e57da50>; contentOffset: {0, 0}>

这显然是调用此函数的问题,因此我将进一步调查,并在获得答案后立即发布我的答案。

现在我将介绍如何调用这个函数:

签署通知:

/**
 *  notifies the view controller that its view is about to be added to a view hierarchy
 *
 *  @param  animated                    whether it will be added in an animated fashion
 */
- (void)viewWillAppear:(BOOL)animated
{
    //  add ourselves as an observer to product purchases
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAHelperProductPurchasedNotification object:nil];
}

发送通知:

/**
 *  handles all the extra work not that a product has been purchased
 *
 *  @param  productIdentifier           the purchased product's identifier
 */
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
    //  adds the product's id to our purchased product ids list
    [_purchasedProductIdentifiers addObject:productIdentifier];

    //  stores this purchase in nsuserdefaults for long term use
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
    //  save the user defaults
    [[NSUserDefaults standardUserDefaults] synchronize];

    //  send notification to tohers for awareness of this purchase
    [[NSNotificationCenter defaultCenter] postNotificationName:IAHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
4

1 回答 1

0

这是一个非常愚蠢的错误:

该通知不断发布,但不是通过我在问题中包含的方法。

这一切都源于通知名称本身。

我已经在 IAPHelper.h 类中对其进行了实例化,如下所示:

//  used to notify listeners when a product is purchased
UIKIT_EXTERN NSString *const IAHelperProductPurchasedNotification;

在实现文件(IAPHelper.m)中,我一直在这样做:

NSString *const IAHelperProductPurchasedNotification;

当我需要这样做时:

NSString *const IAHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification";

奇怪的是,通知只是被重复调用,但是,很难预测其中一些愚蠢的错误的行为。

感谢您花时间帮助我。

于 2012-10-17T11:31:45.753 回答