我一直在试图调出SBSettings切换扭转乾坤或关闭 - 像飞行模式,无线网络,SSH等 - 可我就是不明白,为什么我的代码适用于一些切换,而不是全部。 当然,我只是在谈论“简单”的切换; 带来了自己的窗口,用自己喜欢的控制音量或亮度不切换。 我已经能够成功地打开/关闭3G,数据,我的铃声,在MYWI拨动......之类的东西,但我不明白为什么有些切换的 - 飞行模式,蓝牙,WiFi - 上我的手机将不会对与上面提到的其他东西的工作原理相同的代码响应。
下面是一些我使用的代码:
//.h
#import <UIKit/UIKit.h>
#import <sys/stat.h>
#include "/usr/include/dlfcn.h"
typedef enum toggleTypes
{
SIMPLE,
NOT_SIMPLE
} ToggleTypes;
typedef bool (*BoolFn)();
typedef void (*VoidBoolFn)(bool b);
@interface Toggle : NSObject
@property (strong, nonatomic) NSString *toggleName;
@property (nonatomic) ToggleTypes toggleType;
- (Toggle *) initWithFullPath:(NSString *) togglePath;
- (BOOL) isEnabled;
- (BOOL) isCapable;
- (BOOL) getStateFast;
- (void) setState:(BOOL) state;
@end
//.m
@implementation Toggle {
@private
void *_dylibHandle;
BoolFn _isCapable;
BoolFn _isEnabled;
BoolFn _getStateFast;
VoidBoolFn _setState;
}
@synthesize toggleName = _toggleName;
@synthesize toggleType = _toggleType;
- (Toggle *) initWithFullPath:(NSString *) togglePath
{
self.toggleName = [[togglePath stringByDeletingLastPathComponent] lastPathComponent];
const char *fullName = [togglePath UTF8String];
struct stat fstat;
if( stat(fullName, &fstat) != 0 )
{
NSLog(@"Reading error for file %s", fullName);
return nil;
}
dlerror();
_dylibHandle = dlopen(fullName, RTLD_LAZY | RTLD_LOCAL );
if( !_dylibHandle)
{
NSLog(@"dlopen encountered an error and did not open file: %s", fullName);
return nil;
}
dlerror();
_isCapable = dlsym(_dylibHandle, "isCapable");
char *error = dlerror();
if( !_isCapable )
{
NSLog(@"An error was encountered while loading symbol \"isCapable\"\nFile: %s\nError: %s", fullName, error);
return nil;
}
_isEnabled = dlsym(_dylibHandle, "isEnabled");
error = dlerror();
if( !_isCapable )
{
NSLog(@"An error was encountered while loading symbol \"isEnabled\"\nFile: %s\nError: %s", fullName, error);
return nil;
}
_getStateFast = dlsym(_dylibHandle, "getStateFast");
error = dlerror();
if( !_isCapable )
{
NSLog(@"An error was encountered while loading symbol \"getStateFast\"\nFile: %s\nError: %s", fullName, error);
return nil;
}
_setState = dlsym(_dylibHandle, "setState");
error = dlerror();
if( !_isCapable )
{
NSLog(@"An error was encountered while loading symbol \"getStateFast\"\nFile: %s\nError: %s", fullName, error);
return nil;
}
NSArray *windowsBefore = [[[[UIApplication sharedApplication] windows] objectAtIndex:0] subviews];
[self setState:[self isEnabled]];
NSArray *windowsAfter = [[[[UIApplication sharedApplication] windows] objectAtIndex:0] subviews];
if( [windowsAfter count] > [windowsBefore count] ) {
self.toggleType = NOT_SIMPLE;
for (UIView *view in windowsAfter) {
if( ![windowsBefore containsObject:view] )
[view removeFromSuperview];
}
} else {
self.toggleType = SIMPLE;
}
return self;
}
- (BOOL) isEnabled
{
return _isEnabled();
}
- (BOOL) isCapable
{
return _isCapable();
}
- (BOOL) getStateFast
{
return _getStateFast();
}
- (void) setState:(BOOL) state
{
_setState(state);
}
- (void) dealloc
{
dlclose(_dylibHandle);
}
@end
调用代码initWithFullPath
:
if( [directoryToggleNames objectForKey:toggleName] == nil ) {
if ((strlen(SBTOGGLES_PATH) + strlen([toggleName UTF8String] + strlen("/Toggle.dylib")) + 1) > _POSIX_PATH_MAX) {
NSLog(@"Toggle %@ has a path name that is too long", toggleName);
return nil;
}
char fullName[_POSIX_PATH_MAX + 1];
strcpy(fullName, SBTOGGLES_PATH);
strcat(fullName, [toggleName UTF8String]);
strcat(fullName, "/Toggle.dylib");
NSString *fullPath = [NSString stringWithUTF8String:fullName];
return [[OSToggle alloc] initWithFullPath:fullPath];
}
return [[OSToggle alloc] initWithFullPath:[directoryToggleNames objectForKey:toggleName]];
使用上面的代码与肘类本身的代码:
ToggleScanner *scanner = [ToggleScanner getInstance];
NSDictionary *toggleDict = [self getToggleDictionary];
for (ToggleBase *t in [toggleDict allValues] ) {
OSToggle *toggle = [scanner getToggleByName:t.name];
if( [t.type isEqualToString:@"simple"] ) {
NSLog(@"isCapable: %@; isEnabled: %@", ( [toggle isCapable] ? @"YES" : @"NO" ), ( [toggle isEnabled] ? @"YES" : @"NO" ));
[toggle setState:t.state.boolValue];
NSLog(@"Set toggle, %@, to %@; result isEnabled: %@", t.name, ( t.state.boolValue ? @"YES" : @"NO" ), ( [toggle isEnabled] ? @"YES" : @"NO" ) );
} else {
NSLog(@"Toggle is not a simple type");
}
}
ToggleScanner只是看起来在调出SBSettings切换目录,并建立了基于该切换字典发现的情况。 getToggleDict是,需要一个NSData对象出来CoreData的并把它变成一组切换和状态,我可以反过来执行的方法。 ToggleBase仅仅是一个信息保存类; 它没有任何方法。
它的粗糙,但它的工作原理上的一些切换的。 现在的问题是:有什么是我做错了吗? 无论是飞行模式和3G切换的开/关开关简单,但此代码对3G切换,但不是飞行模式之一。 任何想法,意见和/或建议,这是为什么?
编辑1:添加的代码调用切换类和一些关于它的描述。