NSNotification问题

4

我的类初始化时,会将自己添加为多个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框架包含到我的项目中: enter image description here 我已下载了CoreWLANWirelessManager.app,并将其用作参考。奇怪的是,苹果的代码使用了不推荐使用的通知,但仍然可以工作。我尝试过使用新的API和不推荐使用的API,但都没有成功。我不确定是否可以在这里发布他们的代码,但实际上没有任何区别。选择器甚至具有相同的名称。
请随时要求进一步阐述。
更新(Dustin的回答后):我创建了一个新项目,希望隔离问题。我设置了我的.h和.m文件,就像你描述的那样。可悲的是,我仍然没有收到任何通知。为了向您展示我没有撒谎(或疯了),我包括了两个(相当拥挤的)截图,在同一个运行时拍摄。请注意: (1.我在handleNotification:方法中设置了断点。应用程序从未暂停。 (2.我包括网络窗口,以显示我的Mac在此期间确实更改了Wi-Fi网络。 (3.没有NSLoged
网络1: enter image description here 网络2: enter image description here 更新于2012年5月17日:Dustin的答案是正确的,但Wi-Fi接口名称取决于应用程序运行的硬件。在我的情况下(MacBook Air;没有以太网),我的Wi-Fi是en0而不是en1。以太网是en0。谢谢大家的帮助。

你的目标是哪个版本的MacOS? MacOS 10.5、10.6还是10.7? - Michael Dautermann
你在项目的框架列表中包含了CoreWLAN.framework吗? - Michael Dautermann
是的。如果我能发布图片,我本来会包含在内的 :/ - Tanner Silva
奇怪的是,这正是苹果在其示例中使用的完全相同的代码,并且它完美地工作。 - Tanner Silva
分配/初始化操作是否在主线程上执行? - lukasz
显示剩余2条评论
1个回答

3
为了能够接收到这些通知,您需要持有一个CWInterface实例。您的.h文件应如下所示:
#import <Cocoa/Cocoa.h>
@class CWInterface;

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (retain) CWInterface *wirelessInterface;

@end

那么在你的.m文件中应该是这样的。
#import "AppDelegate.h"
#import <CoreWLAN/CoreWLAN.h>

@implementation AppDelegate

@synthesize window = _window;
@synthesize wirelessInterface;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    [[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];

    self.wirelessInterface = [CWInterface interfaceWithName:@"en1"];
}


-(void) handleNotification:(NSNotification*) notification
{
    NSLog(@"Notification Received");
}

@end

请注意 CWInterface 属性,这是重要的部分。

谢谢你的帮助。不幸的是,这并没有起作用。我已经根据回应更新了我的帖子。尽管如此,我还是很感激你的答案。 - Tanner Silva
如果您在应用程序运行时切换WiFi接口的开/关状态,那么您是否会收到通知? - Dustin
没有。无论我做什么都收不到通知。 - Tanner Silva
已解决。我有一台MacBook Air(没有以太网),所以我必须将CWInterface设置为@"en0"而不是@"en1"。我知道这在MBP或台式机上不起作用,但至少我找到了问题所在。感谢您的帮助。 - Tanner Silva
很好,我想问的是en1是否是您的无线接口。 - Dustin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接