有没有办法在 iOS 中以编程方式打开和关闭蓝牙和/或 wifi? [英] Is there a way to toggle bluetooth and/or wifi on and off programmatically in iOS?

查看:18
本文介绍了有没有办法在 iOS 中以编程方式打开和关闭蓝牙和/或 wifi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来在 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).

当我在不同的位置和使用场景之间移动时,我不断地切换这些功能,现在需要多次点击和访问设置应用程序.我正在寻找创建一个简单的应用程序,它存在于 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆