当我的类初始化时,它会将自己添加为一堆不同 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,这是我正在使用的引用。奇怪的是,Apple 的代码使用了已弃用的通知,但它仍然有效。我尝试使用新的 API 和已弃用的 API,但没有成功。我不确定我是否可以在这里发布他们的代码,但实际上没有区别。选择器甚至具有相同的名称。

请不要犹豫,要求进一步详细说明。

更新(在达斯汀的回答之后):我创建了一个新项目,希望能隔离这个问题。我按照你的描述设置了我的 .h 和 .m 文件。遗憾的是,我仍然没有收到任何通知。为了向您表明我没有说谎(或疯了),我提供了两个(相当拥挤)在同一运行时拍摄的屏幕截图。注意:
(1. 我在 handleNotification: 方法中有一个断点。应用程序从不暂停。
(2. 我包含了网络窗口以显示我的 Mac 在此运行时确实改变了 Wi-Fi 网络。
(3. 没有什么是 NSLoged

网络1:

网络2:

2012 年 5 月 17 日更新:Dustin 的回答是正确的,但 Wi-Fi 接口(interface)名称因应用程序运行的硬件而异。就我而言(MacBook Air;无以太网),我的 Wi-Fi 是 en0 而不是 en1。我设法从我妈妈的 iMac 上获取系统配置 plst 文件,Wi-Fi 被称为 en1。以太网是en0。谢谢大家的帮助。

最佳答案

为了让您获得这些通知,您需要持有 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 属性,这是重要的一点

关于objective-c - NSNotification 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10525858/

10-10 21:06