I\'m looking after some Obj-C sample code for a dynamic UIApplicationShortCutItem
.
Basically, I have three static UIApplicationShortcutItems
an
I've post a simple objective-c example on GitHub that add/remove shortcuts to Home Screen.
You can check it here: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions
I have a method on App Delegate that handles shortcut items (based on another stackoverflow answer that I can't found :( ):
- (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {
BOOL handled = NO;
if (shortcutItem == nil) {
return handled;
}
handled = YES;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Handle Shortcut" message:shortcutItem.type delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[av show];
return handled;
}
It is called by application:didFinishLaunchingWithOptions and application: performActionForShortcutItem whether app is launched or not.
And to add/remove shortcuts on demand:
- (void) addActionToShortCutItems{
NSArray *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
if([existingShortcutItems count]){
NSMutableArray *updatedShortcutItems = [existingShortcutItems mutableCopy];
NSInteger numberOfActions = [existingShortcutItems count];
[updatedShortcutItems addObject:[self createItemNumber:numberOfActions]];
[[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
}else{
[UIApplication sharedApplication].shortcutItems = @[[self createItemNumber:0]];
}
}
- (UIApplicationShortcutItem*)createItemNumber:(NSInteger)number{
UIApplicationShortcutItem *newItem = [[UIApplicationShortcutItem alloc]initWithType:[NSString stringWithFormat:@"type%ld",number]
localizedTitle:[NSString stringWithFormat: NSLocalizedString(@"Action %ld", nil),number]
localizedSubtitle:nil
icon:nil
userInfo:nil];
return newItem;
}
- (void) removeActionToShortCutItems{
NSArray *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
NSMutableArray *updatedShortcutItems = [existingShortcutItems mutableCopy];
[updatedShortcutItems removeObjectAtIndex:[updatedShortcutItems count]-1];
[[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
}
Hope it helps and feedback are welcome!