我看到了很多关于这个问题的提问,但实际上没有人提供真正的答案(需要导入哪些框架、实际代码等)。他们只是说使用私有API会导致您的应用程序被App Store拒绝。
我知道使用私有API会导致我的应用程序被拒绝,但我想知道如何在个人使用情况下做到这一点。(iPhone SDK 3.1.2,iPod touch 2g)
我看到了很多关于这个问题的提问,但实际上没有人提供真正的答案(需要导入哪些框架、实际代码等)。他们只是说使用私有API会导致您的应用程序被App Store拒绝。
我知道使用私有API会导致我的应用程序被拒绝,但我想知道如何在个人使用情况下做到这一点。(iPhone SDK 3.1.2,iPod touch 2g)
我也一直在研究这个问题。你需要在项目中包含bluetoothmanager框架和头文件。它应该在
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library/PrivateFrameworks/BluetoothManager.framework/
如果没有相应的头文件,你需要获取从库生成的.h文件并将其包含到你的项目中。我用谷歌找到了一个,这是其中一个链接:
http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk/include/BluetoothManager/
当这些添加到你的项目后,如果头文件已经在框架中,你的导入应该像这样:
#import <BluetoothManager/BluetoothManager.h>
如果你已经将自己的BluetoothManager.h文件添加到项目中,则可以使用以下方法:
#import "BluetoothManager.h
以下是切换蓝牙的代码:
BluetoothManager *manager = [BluetoothManager sharedInstance];
[manager setEnabled:![manager enabled]];
我已经构建了一个实用程序来完成这个操作,并且它能够工作。请注意,如果你只想创建一个不带任何用户界面的蓝牙切换实用程序并退出,请在XCode中创建一个新项目并使用基于窗口的应用模板。将代码添加到didFinishLaunchingWithOptions方法中,并将 [window makeKeyAndVisible]
替换为 exit(0)
。
您需要确保二进制文件和头文件都在以下路径的PrivateFrameworks文件夹中:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/PrivateFrameworks
这将允许您在应用程序中导入PrivateFrameworks,例如BluetoothManager.framework,而不会出现错误。您可以在线上找到如何获取头文件的方法。这适用于3.1.2+,因为我正在编写一个应用程序,它在我的设备和模拟器上都能完美运行。
如果要在模拟器中进行测试,请使用以下内容:
#if TARGET_IPHONE_SIMULATOR
//This is where simulator code goes that use private frameworks
#else
/* this works in iOS 4.2.1 */
Class BluetoothManager = objc_getClass("BluetoothManager");
id btCont = [BluetoothManager sharedInstance];
[btCont setPowered:YES];
#endif
- (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:1.0f] ;
#endif
return YES ;
}
#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
BOOL currentState = [btCont enabled] ;
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;
}
#endif
以上方法适用于iOS 4.2.3。
XCode 5.1.1
和 iOS 7.1
上运行良好。 - Jonathan F.我认为解决方案是通过调用launchctl系统调用,因为它是启动/停止系统服务的守护进程。
使用 iPhone 5s
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;
无法运行