轉(zhuǎn)自:http://blog.csdn.net/cssmhyl/article/details/7963537
今天寫了一段有關(guān)在iPhone程序中開關(guān)WiFi型號(hào)的代碼,經(jīng)測試運(yùn)行良好。
我想不用我多說大家都應(yīng)該知道以上的功能只能在越獄的設(shè)備中實(shí)現(xiàn)!
好了,閑話稍少敘,進(jìn)入正題:
1.首先要在SpringBoard啟動(dòng)之后,我們要執(zhí)行hook動(dòng)作:
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. 然后實(shí)現(xiàn)我們的HOOK函數(shù),這里我們僅僅是注冊(cè)了兩個(gè)消息:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
&NotificationReceivedCallback, CFSTR("turnOffWiFi"), NULL,
CFNotificationSuspensionBehaviorCoalesce);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
&NotificationReceivedCallback, CFSTR("turnOnWiFi"), NULL,
CFNotificationSuspensionBehaviorCoalesce);
3. 最后我們要響應(yīng)這兩個(gè)信號(hào),也就是實(shí)現(xiàn)我們?cè)诘诙街兄该鞯哪莻€(gè)回調(diào)方法(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];
}
也就是這一步正真的對(duì)WiFi信號(hào)進(jìn)行開關(guān)操作。好了我們的后臺(tái)程序(dynamicLibrary)已經(jīng)編寫完成了。
然后在我們的前臺(tái)程序中我們找個(gè)事件來發(fā)送我們?cè)诤笈_(tái)注冊(cè)的那兩個(gè)消息,例如一個(gè)Button和一個(gè)BOOL值來完成這功能:
BOOL turnOff = YES;
if (turnOn) {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
} else {
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
}
測試設(shè)備:iPhone 3GS
系統(tǒng):iOS 4.3.3
設(shè)備狀態(tài):已越獄
測試結(jié)果:Perfect!
注1:使用相同的方法我們可以啟動(dòng)和關(guān)閉藍(lán)牙:
首先,我們要從BluetoothManager.framework這個(gè)私有庫中dump出BluetoothManager這個(gè)類;
然后,我們就可以調(diào)用這個(gè)類的setPowered:方法啟動(dòng)和關(guān)閉藍(lán)牙了(參數(shù):YES為啟動(dòng)、NO為關(guān)閉)。
帶有.h文件的BluetoothManager.framework請(qǐng)到我的資源里下載
- [cpp] view plaincopyprint?
- #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
經(jīng)過我的測試是可以正常使用的。
注2:我們同樣可以對(duì)飛行模式作開啟和關(guān)閉操作:
首先:我們從SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager這兩個(gè) 類,前者主要負(fù)責(zé)功能的開關(guān),后者則是負(fù)責(zé)UI顯示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到當(dāng)前的 飛行模式狀態(tài),setIsInAirplaneMode:這個(gè)方法來設(shè)置飛行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem來刷新UI顯示狀態(tài)。
|