DarkModeToggle v1.2.1
Enable/disable specific tweaks when toggling dark mode
Compatible with iOS 11.0+
Repo: https://captinc.github.io
How to compile
- 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-master
cd ./DarkModeToggle-master
make package
A .deb will now be in the "DarkModeToggle-master/packages" folder
License
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
Developer documentation
How to programmatically enable/disable DarkModeToggle from another tweak:
- Set
dark
orlight
for thedarkModeState
key 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
Code examples
Before starting, place this at the top of your code:
@interface NSDistributedNotificationCenter : NSNotificationCenter
@end
Enable DarkModeToggle:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionary alloc] init];
}
[prefs setObject:@"dark" forKey:@"darkModeState"];
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState" object:nil userInfo:nil];
Disable DarkModeToggle:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionary alloc] init];
}
[prefs setObject:@"light" forKey:@"darkModeState"];
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState" object:nil userInfo:nil];
Toggle DarkModeToggle to the opposite state:
NSString *plistPath = @"/var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist";
NSMutableDictionary *prefs = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (!prefs) {
prefs = [[NSMutableDictionary alloc] init];
}
NSString *currentState = [prefs objectForKey:@"darkModeState"];
if ([currentState isEqualToString:@"dark"]) {
[prefs setObject:@"light" forKey:@"darkModeState"];
}
else if ([currentState isEqualToString:@"light"]) {
[prefs setObject:@"dark" forKey:@"darkModeState"];
}
[prefs writeToFile:plistPath atomically:YES];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.captinc.darkmodetoggle.updateState" object:nil userInfo:nil];
Detect when DarkModeToggle is toggled:
- (void)oneOfYourMethods {
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(itWasToggled) name:@"com.captinc.darkmodetoggle.stateChanged" object:nil];
}
- (void)itWasToggled {
doSomething();
}