我一直在尝试使用SBSettings切换打开或关闭的东西 - 如飞机模式,WiFi,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:添加了调用切换类的代码及其相关说明。
答案 0 :(得分:1)
我的想法与您最初的想法类似,我尝试连接到SBSetting的每个toggle.dylib以打开/关闭3G,Wi-Fi,MyFi。但是,我只能让它适用于3G切换。我无法弄清楚如何使其适用于MyFi切换。您使用的是什么版本的MyFi?
答案 1 :(得分:0)