我试图让观察者在我的笔记本电脑自动更改其wifi连接时通知我。
我有一个“NetworkProperties”类,用于存储我的连接属性:
@interface NetworkProperties : NSObject
{
@public
CWInterface *wfi;
@private
CWWiFiClient *wfc;
NSString *SSID;
NSString *BSSID;
NSString *phyMode;
NSString *hwAddr;
NSString *securityType;
}
@end
和一个“GUIHandle”类,我尝试处理我的GUI。我还有以下功能来启用/禁用扫描:
@interface GUIHandle : NSObject
{
NetworkProperties *NP;
}
-(IBAction)startScan:(id)sender;
-(IBAction)stopScan:(id)sender;
@end
-(IBAction)startScan:(id)sender
{
done = 0;
NP = [[NetworkProperties alloc] init];
[NP scanNetworkProperties];
[NP->wfi addObserver:self forKeyPath:@"bssid" options:NSKeyValueObservingOptionNew context:nil];
}
-(IBAction)stopScan:(id)sender
{
[NP->wfi removeObserver:self forKeyPath:@"bssid"];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"We are here!\n");
if ([keyPath isEqual:@"bssid"]) {
NSLog(@"Value changed!\n");
}
}
但是,如果我的连接的“bssid”发生变化,我不会收到任何通知。我做错了什么?