Problems with NSNotification

When my class initializes, it adds itself as an observer for many different Wi-Fi notifications. For some reason, the selector does not work when any of these events occur. Any ideas? Thanks in advance.

-(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 in my project: enter image description here

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

Please feel free to contact for further refinement.

UPDATE (after Dustin's answer): I created a new project, hoping to isolate the problem. I installed my .h and .m files in the same way as you described. Unfortunately, I still do not receive any notifications. To show you that I'm not lying (or crazy), I included two (rather crowded) screenshots that were taken during the same work. Please note: (1. I have a breakpoint in the handleNotification method: the application never stops. (2. I turned on the network window to show that my Mac really changed the Wi-Fi networks during this runtime. (3. Nothing not NSLoged

Network 1: enter image description here

Network 2: enter image description here

UPDATE May 17, 2012 Dustin answered correctly, but the name of the Wi-Fi interface varies depending on what equipment the application is running on. In my case (MacBook Air, no ethernet) my Wi-Fi is en0 instead of en1. I managed to capture the plst configuration file from my moms iMac, and the Wi-Fi is called en1. Ethernet - en0. Thank you all for your help.

+4
source share
1 answer

To receive these notifications, you need to hold on to the CWInterface instance. Your .h will 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 it will 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 

Note the CWInterface property, which is an important bit

+3
source

Source: https://habr.com/ru/post/1411662/


All Articles