这就是我所说的地方:
/**
* 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];
}
答案 0 :(得分: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";
奇怪的是,通知只是被反复调用,但唉,很难预测其中一些愚蠢错误的行为。
感谢您抽出宝贵时间帮助我们。