今天為了解決舊項目藕合度悯姊,感覺用前輩是用代理傳值羡藐,感覺不太合理,因為兩個對象本身一點關(guān)系都沒有悯许,就是想監(jiān)聽內(nèi)購成功有回調(diào)而已仆嗦。于是乎,就采用通知先壕,但是在通知使用時瘩扼,碰到坑,后面再細(xì)講垃僚。
一集绰、注冊通知
//增加監(jiān)聽者
[[NSNotificationCenter defaultCenter] addObserver:[GDSDKAppsFlyerManager sharedStanderManager] selector:@selector(noticeFromConsoleGameIAP:) name:@"GDIAPStander" object:nil];
//監(jiān)聽到通知時,做相應(yīng)的動作
-(void)noticeFromNetworkGameIAP:(NSNotification*)info{
}
//取消通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"GDIAPStander" object:nil];
}
二谆棺、發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"GDIAPNetwork" object:nil userInfo:infoDict];
三栽燕、坑
眾所周知,通知是先增加 監(jiān)聽者 才能進行處理通知發(fā)送的相應(yīng)用事件改淑。于是在應(yīng)用啟動(application didFinishLaunchingWith-Options:)時碍岔,就已經(jīng)注冊通知,并且已經(jīng)發(fā)送通知朵夏,但是并不實現(xiàn)監(jiān)聽通知的方法(noticeFromNetworkGameIAP:)蔼啦,直覺告訴我,一定是線程問題仰猖,于是將注冊通知 delay 0.25s询吴,果然可以。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:[GDSDKAppsFlyerManager sharedStanderManager] selector:@selector(noticeFromNetworkGameIAP:) name:@"GDIAPNetwork" object:nil];//GDIAPNetwork
});
return YES;
}