博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
阅读量:4110 次
发布时间:2019-05-25

本文共 2273 字,大约阅读时间需要 7 分钟。

今天写了一段有关在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为关闭)。

经过我的测试是可以正常使用的。

注2:我们同样可以对飞行模式作开启和关闭操作:

首先:我们从SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager这两个 类,前者主要负责功能的开关,后者则是负责UI显示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到当前的 飞行模式状态,setIsInAirplaneMode:这个方法来设置飞行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem来刷新UI显示状态。

转载地址:http://ososi.baihongyu.com/

你可能感兴趣的文章
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
初试visual studio2012的新型数据库LocalDB
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Generate Parentheses--生成匹配括号(重)
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>