本文介绍了NSNotification麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级初始化时,它将自己添加为观察器,以观察一堆不同的Wi-Fi通知.由于某些原因,当这些事情发生时,选择器不会运行.有任何想法吗?提前谢谢你.

When my class initializes, it adds itself as an observer for a bunch of different Wi-Fi notifications. For some reason, the selector isn't running when any of these things happen. Any ideas? Thank you ahead of time.

-(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方法:

UPDATE:Here is the handleNotification method:

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

我已经将CoreWLAN框架包括到我的项目中:

I have included the CoreWLAN framework to my project:

我已经下载了CoreWLANWirelessManager.app,这是我用来参考的内容.奇怪的是,Apple的代码正在使用不赞成使用的通知,并且仍然可以正常使用.我尝试使用新API和不推荐使用的API均未成功.我不确定是否可以在此处发布他们的代码,但实际上没有区别.选择器甚至具有相同的名称.

I've downloaded CoreWLANWirelessManager.app, and this is what I'm using for reference. Oddly enough, Apple's code is using deprecated notifications, and it still works. I've tried using the new API's and the deprecated API's with no success. I'm not sure if I can post their code here, but there is literally no difference. The selector even has the same name.

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

Please don't hesitate to ask for further elaboration.

更新(在达斯汀的回答之后):我创建了一个新项目,希望能够找出问题所在.正如您所描述的,我设置了.h和.m文件.可悲的是,我仍然没有收到任何通知.为了向您展示我没有在撒谎(或发疯),我提供了两个(相当拥挤)在同一运行时期间拍摄的屏幕截图.注意:(1.我在handleNotification:方法中有一个断点.该应用程序永不暂停.(2.我添加了网络窗口,以显示我的Mac 在此运行时确实更改了Wi-Fi网络.(3.什么都不是NSLoged

UPDATE (After Dustin's answer): I've created a new project in hopes to isolate the issue. I set up my .h and .m files just as you described. Sadly, I'm still not getting any notifications. To show you I'm not lying (or crazy), I've included two (fairly crowded) screenshots that were taken during the same runtime. Notice:(1. I have a breakpoint in the handleNotification: method. The app never pauses.(2. I included the network window to show my Mac has indeed changed Wi-Fi networks during this runtime.(3. Nothing is NSLoged

网络1:

网络2:

UPDATE 2012年5月17日:Dustin的回答是正确的,但Wi-Fi接口名称取决于应用程序所运行的硬件.在我的情况下(MacBook Air;没有以太网),我的Wi-Fi是en0而不是en1.我设法从妈妈的iMac中抓取了系统配置plst文件,Wi-Fi称为en1.以太网是en0.谢谢大家的帮助.

UPDATE May 17, 2012: Dustin's answer was correct, but the Wi-Fi interface name varies depending on what hardware the app is running on. In my case, (MacBook Air; no ethernet), my Wi-Fi is en0 instead of en1. I managed to grab the system configuration plst file off my moms iMac, and the Wi-Fi is called en1. Ethernet is en0. Thank you all for your help.

推荐答案

为了获得这些通知,您需要保留CWInterface实例.您的.h看起来像这样

In order for you to get those notifications you need to be holding on to an instance of CWInterface. Your .h would look like this

#import <Cocoa/Cocoa.h>
@class CWInterface;

@interface AppDelegate : NSObject <NSApplicationDelegate>

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

@end

然后,您的.m文件中的内容将如下所示

Then in your .m file would look like this

#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属性,这很重要

Notice the CWInterface property, that's the important bit

这篇关于NSNotification麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:05