有没有办法在iOS中以编程方式打开和关闭蓝牙和/或wifi? [英] Is there a way to toggle bluetooth and/or wifi on and off programmatically in iOS?
问题描述
我正在寻找一种在iOS 4.x设备(iPhone和iPad)上打开和关闭状态之间切换蓝牙和wifi的简单方法。
I am looking for an easy way to toggle both bluetooth and wifi between on and off states on iOS 4.x devices (iPhone and iPad).
我是我在不同的位置和使用场景之间移动时不断切换这些功能,现在需要多次点击并访问Settings App。我希望创建一个简单的应用程序,它存在于Springboard上,我可以点击它,它会关闭wifi,如果它打开,反之亦然,然后立即退出。与用于切换蓝牙状态的应用程序类似。
I am constantly toggling these functions as I move between different locations and usage scenarios, and right now it takes multiple taps and visits to the Settings App. I am looking to create a simple App, that lives on Springboard, that I can just tap and it will turn off the wifi if it's on, and vice versa, then immediately quit. Similarly with an App for toggling bluetooth’s state.
我有开发人员SDK,并且对Xcode和iOS开发很满意,所以很高兴能够编写所需的代码来创建应用程序。我只是不知道哪个私有或非私有API具有简单切换这些设施状态所需的功能。
I have the developer SDK, and am comfortable in Xcode and with iOS development, so am happy to write the required code to create the App. I am just at a loss as to which API, private or not, has the required functionality to simply toggle the state of these facilities.
因为这是非常个人化的痒,我无意尝试销售应用程序或在应用程序商店中获取它,因此符合关于API使用的应用程序指南是没有问题的。我不想做的是越狱设备,因为我想保留核心软件。
Because this is scratching a very personal itch, I have no intent to try and sell the App or get it up on the App store, so conforming with App guidelines on API usage is a non-issue. What I don’t want to do is jailbreak the devices, as I want to keep the core software as shipped.
任何人都可以指点我的一些示例代码或更多信息实现这一目标,因为我的Google-fu让我失望,如果信息出现在4.x设备上,我就找不到它。
Can anyone point me at some sample code or more info on achieving this goal, as my Google-fu is letting me down, and if the information is out there for 4.x devices I just can’t find it.
推荐答案
感谢Matt Farrugia(Twitter上的@mattfarrugia)我正在寻找的答案是:
Thanks to Matt Farrugia (@mattfarrugia on Twitter) the answer I was looking for was:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
#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:0.1f] ;
#endif
return YES ;
}
#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
BOOL currentState = [btCont enabled] ;
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;
exit( EXIT_SUCCESS ) ;
}
#endif
您还需要链接Gamekit框架,只需将此代码添加到新的Xcode项目并在设备上运行。这样做会创建一个应用程序,可以打开和关闭蓝牙。
You need to link against the Gamekit framework as well, but simply add in this code to a new Xcode project and run on the device. Doing so creates a 1-tap App that toggles Bluetooth on and off.
这篇关于有没有办法在iOS中以编程方式打开和关闭蓝牙和/或wifi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!