【发布时间】:2014-04-18 20:40:58
【问题描述】:
我今天使用 theos 创建了我的第一个翻转开关。
开关编辑一个调整的 .plist 文件以将其打开
开关工作并打开调整,但为了使调整对应用程序生效,必须重新启动应用程序
所以我需要让开关在每次打开和关闭时杀死特定的应用程序
顺便说一句,我是 Objective-c 的新手,非常感谢您的帮助,谢谢!
我的开关.x
#import "FSSwitchDataSource.h"
#import "FSSwitchPanel.h"
#import <notify.h>
static NSString * const PREF_PATH = @"/var/mobile/Library/Preferences/file.plist";
static NSString * const kSwitchKey = @"enabled";
@interface waplastseenSwitch : NSObject <FSSwitchDataSource>
@end
@implementation waplastseenSwitch
- (FSSwitchState)stateForSwitchIdentifier:(NSString *)switchIdentifier
{
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:PREF_PATH];
id existEnable = [dict objectForKey:kSwitchKey];
BOOL isenabled = existEnable ? [existEnable boolValue] : YES;
return isenabled ? FSSwitchStateOn : FSSwitchStateOff;
}
- (void)applyState:(FSSwitchState)newState forSwitchIdentifier:(NSString *)switchIdentifier
{
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:PREF_PATH];
NSMutableDictionary *mutableDict = dict ? [[dict mutableCopy] autorelease] : [NSMutableDictionary dictionary];
switch (newState) {
case FSSwitchStateIndeterminate:
return;
case FSSwitchStateOn:
[mutableDict setValue:@YES forKey:kSwitchKey];
break;
case FSSwitchStateOff:
[mutableDict setValue:@NO forKey:kSwitchKey];
break;
}
[mutableDict writeToFile:PREF_PATH atomically:YES];
notify_post("Flipswitch.settingschanged");
}
@end
【问题讨论】:
标签: ios iphone objective-c theos