使用startMonitoringEventWithType:error:努力检测wifi SSID的变化

时间:2014-12-15 23:43:39

标签: macos cocoa osx-yosemite ssid corewlan

Apple似乎在Yosemite和CoreWLAN框架上引入了相当大的变化。我想使用它的新API,引用头文件:

/*!
 * @method
 *
 * @param type
 * A CWEventType value.
 *
 * @param error
 * An NSError object passed by reference, which upon return will contain the error if an error occurs.
 * This parameter is optional.
 *
 * @result
 * A BOOL value indicating whether or not an error occurred. YES indicates no error occurred.
 *
 * @abstract 
 * Register for specific Wi-Fi event notifications.
 * 
 * @discussion
 * Requires the <i>com.apple.wifi.events</i> entitlement.
 */
- (BOOL)startMonitoringEventWithType:(CWEventType)type error:(out NSError **)error NS_AVAILABLE_MAC(10_10);

并将CWEventType设置为:CWEventTypeSSIDDidChange

它说它需要权利,但我无法在我的Mac上运行它。错误消息是:

  

该应用意外退出。来自调试器的消息:由于终止而终止   代码签名错误。

我的权利文件(我怀疑问题在哪里)是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.wifi.events</key>
    <true/>
</dict>
</plist>

我正在目标的构建设置中设置代码签名路径。说到这一点,如果我排除本地权利文件,应用程序会运行,但行为不符合预期。正在研究的API返回一个错误对象,其中包含以下描述:

Error Domain=com.apple.wifi.request.error Code=4 "The operation couldn’t be completed. (com.apple.wifi.request.error error 4.)"

这绝对是一个令人头疼的事,或者至少我希望不然,我是一个完全白痴。我在会员中心为我的应用程序提供了特定的应用程序ID,以及特定的开发配置文件(尽管我不应该使用通配符开发配置文件)。

提前致谢。

3 个答案:

答案 0 :(得分:4)

目前(2015年7月31日)似乎存在CWWiFiClient中的错误:未正确授予权利。这甚至延伸到非沙盒应用程序。有关详细信息,请参阅Apple开发人员论坛上的this question

因此,我们可能不得不暂时使用已弃用的API。 syammala提供了good example如何使用已弃用的API。

答案 1 :(得分:2)

这与你想要达到的目标相同。它会在SSID更改时通知您

为了获得这些通知,您需要持有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

使用接口名称en1或en0时要小心。通过给出ifconfig

来查看存在哪个接口ip来检查您的系统

答案 2 :(得分:1)

根据CWEventDelegate的文档,应将CWEventDelegate与startMonitoringEventWithType一起使用: https://developer.apple.com/documentation/corewlan/cweventdelegate

整个代码是:

- (void)testDelegateMethod{
    [CWWiFiClient sharedWiFiClient].delegate = self;
    
    NSError *error;
    [[CWWiFiClient sharedWiFiClient] startMonitoringEventWithType:CWEventTypePowerDidChange error:&error];
    [[CWWiFiClient sharedWiFiClient] startMonitoringEventWithType:CWEventTypeSSIDDidChange error:&error];
    [[CWWiFiClient sharedWiFiClient] startMonitoringEventWithType:CWEventTypePowerDidChange error:&error];
    [[CWWiFiClient sharedWiFiClient] startMonitoringEventWithType:CWEventTypeLinkDidChange error:&error];
    [[CWWiFiClient sharedWiFiClient] startMonitoringEventWithType:CWEventTypeNone error:&error];
    
    if (error) {
        NSLog(@"error : %@",error);
    }
}

#pragma mark - CWEventDelegate
- (void)clientConnectionInterrupted{
    NSLog(@"-- clientConnectionInterrupted");
}

- (void)clientConnectionInvalidated{
    
    NSLog(@"-- clientConnectionInvalidated");
}


- (void)powerStateDidChangeForWiFiInterfaceWithName:(NSString *)interfaceName{
    NSLog(@"-- %@ powerStateDidChange  ",interfaceName);
}

- (void)ssidDidChangeForWiFiInterfaceWithName:(NSString *)interfaceName{
    NSLog(@"-- %@ ssidDidChange",interfaceName);
}