I'm looking after some Obj-C sample code for a dynamic UIApplicationShortCutItem
.
Basically, I have three static UIApplicationShortcutItems
and I only want to display them under certain conditions. I presume that you can't change the visible status of a static UIApplicationShortcutItem
, so I'm looking after a simple way to add dynamic UIApplicationShortcutItem
s.
You can use the following code to add shortcutitem for you app dynamic:
UIApplicationShortcutIcon * photoIcon = [UIApplicationShortcutIcon iconWithTemplateImageName: @"selfie-100.png"]; // your customize icon
UIApplicationShortcutItem * photoItem = [[UIApplicationShortcutItem alloc]initWithType: @"selfie" localizedTitle: @"take selfie" localizedSubtitle: nil icon: photoIcon userInfo: nil];
UIApplicationShortcutItem * videoItem = [[UIApplicationShortcutItem alloc]initWithType: @"video" localizedTitle: @"take video" localizedSubtitle: nil icon: [UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypeCaptureVideo] userInfo: nil];
[UIApplication sharedApplication].shortcutItems = @[photoItem,videoItem];
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 <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
if([existingShortcutItems count]){
NSMutableArray <UIApplicationShortcutItem *> *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 <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];
[updatedShortcutItems removeObjectAtIndex:[updatedShortcutItems count]-1];
[[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
}
Hope it helps and feedback are welcome!
Here's how to detect if the app was launched with a quick shortcut in Objective-c.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem){
[self handleShortCutItem:shortcutItem];
}
}
- (void)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {
if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
//ACTION HERE
}
}
To detect the type of shortcut selected while the app is running in the background.
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"%@", shortcutItem.type);
if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
//ACTION HERE
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With