我对通知非常熟悉,但在使用应用程序内购买后我无缘无故崩溃(我怀疑它与它有关)。
因此,当用户完成购买时,将调用此函数:
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
// i get the crash here when trying to post the notification.
}
现在,具有观察者的主场景在开头设置:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];
是因为观察者将对象设置为nil吗?它应该是什么?
答案 0 :(得分:1)
我建议将Info放入userInfo中,如下所示:
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:nil userInfo:@{@"identifier": productIdentifier}];
// i get the crash here when trying to post the notification.
}
然后你可以用
观察NSNotification[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];
在-(void) productPurchased:(NSNotification*)notification
中,您可以收到信息:
-(void) productPurchased:(NSNotification*)notification {
NSString *productIdentifier = [notification.userInfo valueForKey:@"identifier"];
}
答案 1 :(得分:0)
可能会出现此问题,因为您将nil对象设为NSNotification observer
。
当对象解除分配时,通常可以从NSNotificationCenter的观察者列表中删除self。
添加此
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在作为通知观察者的所有类中。 (请注意,可能有其他地方要删除观察者。例如,在viewDidDisappear中)