iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
2015-08-24 17:18
351 查看
转自:http://www.cnblogs.com/OtionSky/archive/2011/11/08/iOS_WiFi_Controller.html
今天写了一段有关在iPhone程序中开关WiFi型号的代码,经测试运行良好。
我想不用我多说大家都应该知道以上的功能只能在越狱的设备中实现!
好了,闲话稍少叙,进入正题:
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) {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
} else {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
}
测试设备:iPhone 3GS
系统:iOS 4.3.3
设备状态:已越狱
测试结果:Perfect!
注1:使用相同的方法我们可以启动和关闭蓝牙:
首先,我们要从BluetoothManager.framework这个私有库中dump出BluetoothManager这个类;
然后,我们就可以调用这个类的setPowered:方法启动和关闭蓝牙了(参数:YES为启动、NO为关闭)。
带有.h文件的BluetoothManager.framework请到我的资源里下载
经过我的测试是可以正常使用的。
注2:我们同样可以对飞行模式作开启和关闭操作:
首先:我们从SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager这两个 类,前者主要负责功能的开关,后者则是负责UI显示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到当前的 飞行模式状态,setIsInAirplaneMode:这个方法来设置飞行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem来刷新UI显示状态。
今天写了一段有关在iPhone程序中开关WiFi型号的代码,经测试运行良好。
我想不用我多说大家都应该知道以上的功能只能在越狱的设备中实现!
好了,闲话稍少叙,进入正题:
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) {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
} else {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
}
测试设备:iPhone 3GS
系统:iOS 4.3.3
设备状态:已越狱
测试结果:Perfect!
注1:使用相同的方法我们可以启动和关闭蓝牙:
首先,我们要从BluetoothManager.framework这个私有库中dump出BluetoothManager这个类;
然后,我们就可以调用这个类的setPowered:方法启动和关闭蓝牙了(参数:YES为启动、NO为关闭)。
带有.h文件的BluetoothManager.framework请到我的资源里下载
#import <UIKit/UIKit.h> #import <BluetoothManager/BluetoothManager.h> //#import <SpringBoard/SBWiFiManager.h> @interface hylViewController : UIViewController{ BOOL isBlueToothOn; BOOL isWlanOn; } @end
#import "hylViewController.h" @implementation hylViewController #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; Class BluetoothManager = objc_getClass( "BluetoothManager" ) ; id btCont = [BluetoothManager sharedInstance] ; isBlueToothOn = [btCont enabled] ; UISwitch *blueTouthSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(150,50,0,0)]; [blueTouthSwitch addTarget:self action:@selector(BlueTouthSwitchAction:) forControlEvents:UIControlEventValueChanged]; blueTouthSwitch.on = isBlueToothOn; [self.view addSubview:blueTouthSwitch]; [blueTouthSwitch release]; } -(void)BlueTouthSwitchAction:(id)sender { #if TARGET_IPHONE_SIMULATOR exit( EXIT_SUCCESS ) ; #else /* this works in iOS 4.2.3 */ Class BluetoothManager = objc_getClass( "BluetoothManager" ); id btCont = [BluetoothManager sharedInstance]; [self performSelector:@selector(toggle:) withObject:btCont afterDelay:1.0f]; #endif } #if TARGET_IPHONE_SIMULATOR #else - (void)toggle:(id)btCont { BOOL currentState = [btCont enabled] ; [btCont setEnabled:!currentState] ; [btCont setPowered:!currentState] ; } #endif @end
经过我的测试是可以正常使用的。
注2:我们同样可以对飞行模式作开启和关闭操作:
首先:我们从SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager这两个 类,前者主要负责功能的开关,后者则是负责UI显示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到当前的 飞行模式状态,setIsInAirplaneMode:这个方法来设置飞行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem来刷新UI显示状态。
相关文章推荐
- iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
- 008 [iOS图像]一、iOS中的那些图片格式
- Activity随手势滑动退出,及模糊Dialog(类似于IOS的效果)
- iOS自动化常用命令集合
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- IOS集成支付宝回调的坑
- ios开发里的第三方高级框架神器
- 【iOS】苹果,百度Map定位使用与总结
- ios自定义对象(或自定义数据类型)归档
- ios 开发 收起键盘的小技巧
- IOS 多线程(1) --基础知识
- iOS开发学习之内存管理笔记简述
- iOS动画原理
- IOS GIF图片解析 将gif图片解析成image数组
- IOS的socket开发基础
- [iOS开发日记]简易计算器
- iOS中设置同一文本的不同颜色值
- IOS-笔记13(Closures, Multithreading)
- iOS 画分割线的问题
- iOS第三方开源库的吐槽和备忘