0

I have an app where it makes sense to update UIApplicationShortcutItems dynamically. The catch is that the specific set of shortcut items depends on the state of the network (think reachability, captive portal and VPN status, etc).

It's easy to update shortcut items when the app is in the foreground, but I don't see any obvious and appropriate time to do so when the app is in the background... and, of course, the network state can very easily change then, too.

Am I missing something, or do I need to wait until iOS 10? :-)

Dave Peck
  • 1,342
  • 1
  • 17
  • 24
  • This appears to be the answer to your question: http://stackoverflow.com/a/32817011/22147 – Rhythmic Fistman Oct 16 '15 at 01:40
  • Possible duplicate of [Sample code on dynamic UIApplicationShortcutItems (Objective-C)](http://stackoverflow.com/questions/32805743/sample-code-on-dynamic-uiapplicationshortcutitems-objective-c) – Rhythmic Fistman Oct 16 '15 at 01:40

1 Answers1

0

I think the question could be broken down into two parts: background execution & updating shortcut items dynamically.

IMHO, iOS background execution is still quite limited, like: executing finite-length tasks, or only specific app types are allowed to run in the background for longer time. Reachability doesn't really fit long running background task case.

As you mentioned, updating shortcut items dynamically is pretty easy (as below):

// replace updateShortcutItemMethod and EventYouAreInterestedIn accordingly
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(<updateShortcutItemMethod>) name:<EventYouAreInterestedIn> object:nil];

- (void)updateShortcutItemWithType:(NSString *)type title:(NSString *)title subtitle:(NSString *)subtitle icon:(NSString *)icon
{
    NSMutableArray *shortcutItems = [NSMutableArray arrayWithArray:[UIApplication sharedApplication].shortcutItems];
    for (UIMutableApplicationShortcutItem *shortcutItem in shortcutItems) {
        if ([shortcutItem.type isEqualToString:type]) {
            UIMutableApplicationShortcutItem *updatedShortcutItem = [[UIMutableApplicationShortcutItem alloc] initWithType:type localizedTitle:title localizedSubtitle:subtitle icon:[self getIconFrom:icon] userInfo:nil];
            NSUInteger index = [shortcutItems indexOfObject:shortcutItem];
            [shortcutItems replaceObjectAtIndex:index withObject:updatedShortcutItem];
            [UIApplication sharedApplication].shortcutItems = shortcutItems;
        }
    }
}

And apart from the technical aspect, according to the user experience point of view, updating UIApplicationShortcutItem when application is in background is not really a good idea.

Because when application is in background, user is not in the context of the application (no interaction occurs). Changing UIApplicationShortcutItem too often while user is not aware of why, could confuse user a lot. Better way of doing it should be informing user explicitly what is happening while user is interacting with the application.

Sorry, this may not really help achieve what you want.

Jing Li
  • 14,547
  • 7
  • 57
  • 69
  • Never assume that people don't have a good reason to do what they are asking for. That's rude. An example of dynamically updating shortcut items would be when they should change based on the current date (that's the case I'm currently working on). I'm sure there are other time dependent use cases as well as situations where you might update based on other things, like location, or something over the internet. – WolfLink Oct 24 '16 at 07:01