4

私のクラスが初期化されると、さまざまなWi-Fi通知のオブザーバーとして自分自身を追加します。何らかの理由で、これらのいずれかが発生したときにセレクターが実行されていません。何か案は?よろしくお願いします。

-(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をダウンロードしました。これは、参照用に使用しているものです。奇妙なことに、Appleのコードは非推奨の通知を使用しており、それでも機能します。新しいAPIと非推奨のAPIを使用してみましたが、成功しませんでした。ここに彼らのコードを投稿できるかどうかはわかりませんが、文字通り違いはありません。セレクターも同じ名前です。

遠慮なく詳細をお尋ねください。

更新(ダスティンの回答後):問題を切り分けることを期待して、新しいプロジェクトを作成しました。あなたが説明したように、.hファイルと.mファイルを設定しました。残念ながら、まだ通知が届きません。私が嘘をついていない(または狂っていない)ことを示すために、同じランタイム中に撮影された2つの(かなり混雑した)スクリーンショットを含めました。注意:(1。handleNotification:メソッドにブレークポイントがあります。アプリは一時停止しません。(2。このランタイム中にMac実際にWi-Fiネットワークを変更したことを示すためにネットワークウィンドウを含めました。(3。NSLogedはありません)

ネットワーク1: ここに画像の説明を入力してください

ネットワーク2: ここに画像の説明を入力してください

更新2012年5月17日:ダスティンの答えは正しかったが、Wi-Fiインターフェース名は、アプリが実行されているハードウェアによって異なります。私の場合(MacBook Air、イーサネットなし)、Wi-Fiはen1ではなくen0です。お母さんのiMacからシステム構成のplstファイルを取得できました。Wi-Fiはen1と呼ばれています。イーサネットはen0です。よろしくお願いします。

4

1 に答える 1

3

これらの通知を受け取るには、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プロパティに注意してください。これは重要なビットです。

于 2012-05-12T13:46:23.247 に答える