我的类初始化时,会将自己添加为多个Wi-Fi通知的观察者。但不知何故,当这些事件发生时,选择器并未运行。有什么想法吗?提前感谢。
-(id) init
{
if (self)
{
sself = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWModeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWBSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWCountryCodeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWLinkDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWPowerDidChangeNotification object:nil];
更新: 这是handleNotification方法:
-(void) handleNotification:(NSNotification*) notification
{
NSLog(@"Notification Received");
}
我已将CoreWLAN框架包含到我的项目中: 我已下载了CoreWLANWirelessManager.app,并将其用作参考。奇怪的是,苹果的代码使用了不推荐使用的通知,但仍然可以工作。我尝试过使用新的API和不推荐使用的API,但都没有成功。我不确定是否可以在这里发布他们的代码,但实际上没有任何区别。选择器甚至具有相同的名称。
请随时要求进一步阐述。
更新(Dustin的回答后):我创建了一个新项目,希望隔离问题。我设置了我的.h和.m文件,就像你描述的那样。可悲的是,我仍然没有收到任何通知。为了向您展示我没有撒谎(或疯了),我包括了两个(相当拥挤的)截图,在同一个运行时拍摄。请注意: (1.我在handleNotification:方法中设置了断点。应用程序从未暂停。 (2.我包括网络窗口,以显示我的Mac在此期间确实更改了Wi-Fi网络。 (3.没有NSLoged
网络1: 网络2: 更新于2012年5月17日:Dustin的答案是正确的,但Wi-Fi接口名称取决于应用程序运行的硬件。在我的情况下(MacBook Air;没有以太网),我的Wi-Fi是en0而不是en1。以太网是en0。谢谢大家的帮助。