Using startMonitoringEventWithType: ошибка: в попытке обнаружить изменение SSID Wi-Fi
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.)"
Это определенно умный твистер, или, по крайней мере, я надеюсь, что в противном случае я полный идиот. У меня есть определенный идентификатор приложения для моего приложения в Центре-члене, а также определенный профиль разработки (хотя мне не нужно, поскольку я использую профиль шаблона шаблона).
Спасибо заранее.
Ответы
Ответ 1
Кажется, что в настоящее время (31 июля 2015 года) есть ошибка в CWWiFiClient
: права не предоставляются должным образом. Это даже распространяется на приложения, отличные от песочницы. См. этот вопрос на форумах разработчиков Apple для получения дополнительной информации.
В результате нам может потребоваться использовать устаревший API. syammala предоставляет хороший пример того, как использовать устаревший API.
Ответ 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. Проверьте свой sysytem, посмотрев на какой интерфейс ip присутствует, указав ifconfig
Ответ 3
вы должны использовать CWEventDelegate вместе с startMonitoringEventWithType, в соответствии с документом CWEventDelegate: 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);
}