Implementing Home Screen Quick Actions, Dynamic shortcutItems – iOS9 Feature (Part 3)

My previous post was for giving  the general information on implementing home screen quick actions by the way of using static shortcut items in the info.plist of project. To read general concept and methods, this way please.

Despite of the static shortcut items can not be changed regarding to different cases, dynamic shortcut items can be changed case by case in any wherever or whenever in application running cycle. You can add or remove your dynamic shortcut items according to personal data, login status etc., Also you can reset properties 0f your dynamic shortcut items, such as title appropriately to localization settings of your application while static shortcut items’ localized titles are managed by operating system language. Only one disadvantage of dynamic ones is that you can not initialize them before the first launch of app.

For initialising shortCutItems:

-(void) initializeShortCutItemList{

   if(!self.forceTouchItemInitialized && [[UIApplication sharedApplication] respondsToSelector:@selector(shortcutItems)]){
      NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
      NSMutableArray <UIApplicationShortcutItem *> *shortcutItems = [existingShortcutItems mutableCopy];

      UIMutableApplicationShortcutItem *appShortCutItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"appShortCutItemTypeOne" localizedTitle:[self getLocalizedStringForShorcutItem:@"appShortCutItemTypeOne"]];
      appShortCutItem.icon =  UIApplicationShortcutIconTypeCompose;

      UIMutableApplicationShortcutItem *appShortCutItem2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"appShortCutItemTypeTwo" localizedTitle:[self getLocalizedStringForShorcutItem:@"appShortCutItemTypeTwo"]];
      appShortCutItem2.icon =  UIApplicationShortcutIconTypeCompose;

      [shortcutItems addObject:appShortCutItem];
      [shortcutItems addObject:appShortCutItem2];

      [[UIApplication sharedApplication] setShortcutItems: shortcutItems];
      self.forceTouchItemInitialized = YES;
   }
}

You can reach shortCutItems through [UIApplication sharedApplication]. You should get shortCutItems’ mutableCopy cause of shortCutItems can not modified, only you use get or set method on shortCutItems. The same is for UIApplicationShortCutItem.

For updating existingShortCutItems:

-(void) updateQuickActionItemList{

if([UIApplication sharedApplication] respondsToSelector:@selector(shortcutItems)){
   NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
   NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];

   for(NSUInteger anIndex=0; anIndex<[existingShortcutItems count]; anIndex++){
      UIApplicationShortcutItem *anExistingShortcutItem = [existingShortcutItems objectAtIndex: anIndex];
      UIMutableApplicationShortcutItem *aMutableShortcutItem = [anExistingShortcutItem mutableCopy];
      [aMutableShortcutItem setLocalizedTitle: [self getLocalizedStringForShorcutItem:anExistingShortcutItem.type]];
      [updatedShortcutItems replaceObjectAtIndex: anIndex withObject: aMutableShortcutItem];
   }

   [[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
}
}
-(NSString*) getLocalizedStringForShorcutItem:(NSString*) shortcutItemType{

   if([shortcutItemType isEqualToString:@"appShortCutItemTypeOne"])   {
      return NSLocalizedString(@"appShortCutItemTypeOneStringKey");
   }else if([shortcutItemType isEqualToString:@"appShortCutItemTypeTwo"]){
      return NSLocalizedString(@"appShortCutItemTypeTwoStringKey");
   }
   return @"";
}

This little following line is curtial for not effecting your users who are using iOS7 or iOS8.

[[UIApplication sharedApplication] respondsToSelector:@selector(shortcutItems)]

As the user taps on a dynamic shortcut item, delegate method on AppDelegate get called is the same with static shortcut items.

– (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler

As a quick note, Apple doesn’t recommend to use adding and removing dynamic shortcut items so many times because of it can cause unpredictable behaviour of application to your users.

But it can be useful to make clever shortcuts for your application.

This is how Instagram uses application shortcuts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s