NSNotification troubles

[亡魂溺海] 提交于 2019-12-04 03:54:46

问题


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

UPDATE: Here is the handleNotification method:

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

I have included the CoreWLAN framework to my project:

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.

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

Network 1:

Network 2:

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.


回答1:


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

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

Notice the CWInterface property, that's the important bit



来源:https://stackoverflow.com/questions/10525858/nsnotification-troubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!