/DarkModeToggle

Enable/disable specific tweaks when toggling dark mode

Primary LanguageObjective-C

DarkModeToggle v1.2.1

Enable/disable specific tweaks when toggling dark mode

Compatible with iOS 11.0+

Repo: https://captinc.github.io

Direct .deb download

Reddit post

Screenshots

How to compile

  1. Install theos on your Mac
  2. Make sure you installed the iOS 11.2 patched SDK for theos
  3. git clone https://github.com/captinc/DarkModeToggle.git ./DarkModeToggle-master
  4. cd ./DarkModeToggle-master
  5. 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:

  1. Set dark or light for the darkModeState key in /var/mobile/Library/Preferences/com.captinc.darkmodetoggle.plist
  2. Post this NSDistributedNotification: com.captinc.darkmodetoggle.updateState

How to programmatically detect when DarkModeToggle is toggled:

  1. 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();
}