Skip to content

Instantly share code, notes, and snippets.

@MTACS
Last active November 20, 2021 11:20
Show Gist options
  • Save MTACS/8e26c4f430b27d6a1d2a11f0a828f250 to your computer and use it in GitHub Desktop.
Save MTACS/8e26c4f430b27d6a1d2a11f0a828f250 to your computer and use it in GitHub Desktop.
Add custom shortcut item to 3D Touch menus on iOS 13+
// MenuItems.m
// (*.m) for gist formatting
// Makefile
TWEAK_NAME = YourTweak
YourTweak_PRIVATE_FRAMEWORKS = SpringBoardServices
// Tweak.h
@interface SBSApplicationShortcutIcon: NSObject
@end
@interface SBSApplicationShortcutItem : NSObject
@property (nonatomic, retain) NSString *type;
@property (nonatomic, copy) NSString * localizedTitle;
@property (nonatomic, copy) SBSApplicationShortcutIcon * icon;
@property (nonatomic, copy) NSString * bundleIdentifierToLaunch;
- (void)setIcon:(SBSApplicationShortcutIcon *)arg1;
@end
@interface SBSApplicationShortcutCustomImageIcon : SBSApplicationShortcutIcon
@property (nonatomic, readwrite) BOOL isTemplate;
- (id)initWithImagePNGData:(id)arg1;
- (BOOL)isTemplate;
@end
// Tweak.xm
%hook SBIconView
- (void)setApplicationShortcutItems:(NSArray *)arg1 {
NSMutableArray *newItems = [[NSMutableArray alloc] init];
for (SBSApplicationShortcutItem *item in arg1) {
[newItems addObject:item];
}
NSData *lightData = UIImagePNGRepresentation([[UIImage imageWithContentsOfFile:@"/Library/Application Support/Tweak.bundle/light.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
NSData *darkData = UIImagePNGRepresentation([[UIImage imageWithContentsOfFile:@"/Library/Application Support/Tweak.bundle/dark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
SBSApplicationShortcutItem *newItem = [%c(SBSApplicationShortcutItem) alloc];
newItem.localizedTitle = @"New Title";
newItem.localizedSubtitle = @"New Subtitle";
newItem.type = @"com.yout.tweak.itemIdentifier";
SBSApplicationShortcutCustomImageIcon *lightIcon = [[SBSApplicationShortcutCustomImageIcon alloc] initWithImagePNGData:lightData];
SBSApplicationShortcutCustomImageIcon *darkIcon = [[SBSApplicationShortcutCustomImageIcon alloc] initWithImagePNGData:darkData];
if (/* Check for dark mode */ == YES) {
[newItem setIcon:darkIcon];
} else {
[newItem setIcon:lightIcon];
}
[newItems addObject:newItem];
%orig(newItems);
}
+ (void)activateShortcut:(SBSApplicationShortcutItem *)item withBundleIdentifier:(NSString *)bundleID forIconView:(SBIconView *)iconView {
if ([[item type] isEqualToString:@"com.your.tweak.itemIdentifier"]) {
// Do select action here
} else {
%orig;
}
}
%end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment