Enable/disable specific tweaks when toggling dark mode
Compatible with iOS 11.0+
Repo: https://captinc.github.io
- Install theos on your Mac
- Make sure you installed the iOS 11.2 patched SDK for theos
git clone https://github.com/captinc/DarkModeToggle.git ./DarkModeToggle-mastercd ./DarkModeToggle-mastermake package
A .deb will now be in the "DarkModeToggle-master/packages" folder
Please do not verbatim copy my tweak, call it your own, and redistribute it. You can use individual parts of my code for your own non-commercial projects. There is no warranty. If you have any questions, PM me on Reddit
How to programmatically enable/disable DarkModeToggle from another tweak:
- Set
darkorlightfor thedarkModeStatekey in/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist - Post this NSDistributedNotification:
com.captinc.darkmodetoggle.updateState
How to programmatically detect when DarkModeToggle is toggled:
- Listen for this NSDistributedNotification:
com.captinc.darkmodetoggle.stateChanged
Note: because my tweak uses NSDistributedNotifications instead of NSNotifications, you can enable/disable/detect DarkModeToggle from any process
Before starting, place this at the top of your code:
@interfaceNSDistributedNotificationCenter : NSNotificationCenter@endEnable DarkModeToggle:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionarydictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionaryalloc] init];
}
[prefs setObject:@"dark"forKey:@"darkModeState"];
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenterdefaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState"object:niluserInfo:nil];Disable DarkModeToggle:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionarydictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionaryalloc] init];
}
[prefs setObject:@"light"forKey:@"darkModeState"];
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenterdefaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState"object:niluserInfo:nil];Toggle DarkModeToggle to the opposite state:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionarydictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionaryalloc] init];
}
NSString *currentState = [prefs objectForKey:@"darkModeState"];
if ([currentState isEqualToString:@"dark"]) {
[prefs setObject:@"light"forKey:@"darkModeState"];
}
elseif ([currentState isEqualToString:@"light"]) {
[prefs setObject:@"dark"forKey:@"darkModeState"];
}
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenterdefaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState"object:niluserInfo:nil];Detect when DarkModeToggle is toggled:
- (void)oneOfYourMethods {
[[NSDistributedNotificationCenterdefaultCenter] addObserver:selfselector:@selector(itWasToggled) name:@"com.captinc.darkmodetoggle.stateChanged"object:nil];
}
- (void)itWasToggled {
doSomething();
}