Search code examples
objective-cios9

Sample code on dynamic UIApplicationShortcutItems


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 UIApplicationShortcutItems.


Solution

  • 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];