我一直在尝试使用 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 是一种从 CoreData 中取出 NSData 对象并将其转换为一组切换和状态的方法,我可以依次执行这些切换和状态。ToggleBase 只是一个信息保存类;它没有方法。
它很粗糙,但它适用于某些切换。问题是:我在这里做错了什么吗?飞行模式和 3G 切换都是简单的开/关开关,但此代码适用于 3G 切换,但不适用于飞行模式之一。关于为什么会这样的任何想法、评论和/或建议?
编辑 1:添加了调用切换类的代码和一些关于它的描述。