当我的类初始化时,它将自己添加为一堆不同Wi通知的观察者。由于某种原因,当这些事情发生时,选择器没有运行。有什么想法吗?提前谢谢你。
-(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,但没有成功。我不确定我是否可以在这里发布他们的代码,但实际上没有区别。选择器甚至有相同的名称。
请不要犹豫,要求进一步详细说明。
更新(在达斯汀回答后):我创建了一个新项目,希望能隔离这个问题。正如您所描述的,我设置了我的.h和.m文件。遗憾的是,我还是没有收到任何通知。为了向您展示我没有说谎(或疯狂),我包含了两个(相当拥挤的)屏幕截图,这些截图都是在同一个运行时拍摄的。注意:(1. handleNotification:方法中有一个断点。这个应用程序从不停顿。(2 )我加入了网络窗口,以显示我的Mac在这个运行时确实改变了Wi网络。(3.没有什么是NSLoged
网络1:

网络2:

2012年5月17日更新:达斯汀的回答是正确的,但Wi接口的名称因应用程序运行的硬件而异。在我的例子中(MacBook Air;没有以太网),我的Wi是en0而不是en1。我设法从我妈妈的iMac上抓取了系统配置plst文件,而Wi被称为en1。以太网是en0。谢谢你们的帮助。
发布于 2012-05-12 13:46:23
为了获得那些通知,您需要保存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属性,这是重要的部分
https://stackoverflow.com/questions/10525858
复制相似问题