有没有一种方法来打开和关闭在编程的iOS蓝牙和/或WiFi? [英] Is there a way to toggle bluetooth and/or wifi on and off programatically in iOS?

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

问题描述

我要寻找一个简单的方法来打开(iPhone和iPad)之间和关闭在iOS 4.x的设备状态蓝牙和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).

我不断地切换这些功能,因为我不同的位置和使用场景之间移动,而现在它需要多个水龙头和参观设置应用。我期待创建一个简单的应用程序,即住在跳板,我可以只需轻按,如果它是对,反之,则立即退出它会关闭WiFi功能。同样有一个App切换蓝牙的状态。

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开发包,以及我在X code和与iOS开发舒服,所以很高兴地写所需的code创建的应用程序。我只是茫然去哪家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.

由于这是刮伤一个非常个人的痒,我也没有打算尝试出售的App或得到它在App Store,所以对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.

任何人都可以点我一些示例code或实现这一目标的详细信息,如我的谷歌福是让我失望,如果信息在那里为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.

推荐答案

感谢马特·法鲁贾(在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框架,以及链接,而只是在这个code添加到一个新的X code项目和设备上运行。这样就会创建一个1抽头应用了蓝牙切换和关闭。

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天全站免登陆