1.首先要在SpringBoard启动之后,我们要执行hook动作:
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
if ([identifier isEqualToString:@"com.apple.springboard"]) {
Class $SpringBoard = (objc_getClass(“SpringBoard”));
_SpringBoard$applicationDidFinishLaunching$ = MSHookMessage($SpringBoard, @selector(applicationDidFinishLaunching:), &$SpringBoard$applicationDidFinishLaunching$);
}
2. 然后实现我们的HOOK函数,这里我们仅仅是注册了两个消息:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
&NotificationReceivedCallback, CFSTR(“turnOffWiFi”), NULL,
CFNotificationSuspensionBehaviorCoalesce);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
&NotificationReceivedCallback, CFSTR(“turnOnWiFi”), NULL,
CFNotificationSuspensionBehaviorCoalesce);
3. 最后我们要响应这两个信号,也就是实现我们在第二步中指明的那个回调方法(NotificationReceivedCallback)
static void NotificationReceivedCallback(CFNotificationCenterRef center,
void *observer, CFStringRef name,
const void *object, CFDictionaryRef
userInfo)
{
BOOL offOrOn = YES;
if ([(NSString *)name isEqualToString:@"turnOffWiFi"]) {
offOrOn = NO;
} else if ([(NSString *)name isEqualToString:@"turnOnWiFi"]) {
offOrOn = YES;
}
[[objc_getClass("SBWiFiManager") sharedInstance] setWiFiEnabled:offOrOn];
}
也就是这一步正真的对WiFi信号进行开关操作。好了我们的后台程序(dynamicLibrary)已经编写完成了。
然后在我们的前台程序中我们找个事件来发送我们在后台注册的那两个消息,例如一个Button和一个BOOL值来完成这功能:
BOOL turnOff = YES;
if (turnOn) {
notify_post(“turnOffWiFi”);
} else {
notify_post(“turnOnWiFi”);
}