Creating shortcut for the app crashes on previous version

941 Views Asked by At

I've created a shortcutItem in an App and when i run in ios 9.2 it works fine. But when I open the app which has ios 8.1 it crashes.

Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)

How is the shortcutItem are created say if i create shortcutItem icon and title dynamically after (launchOption == nil) return YES Does the phone shows shortcutItems then?(because createShortcutItem is not called it should not show is what i think.) Will i be able to open shortcutItem once I opened the app is opened and minimized even though the shortcutItems and icons are not called in didFinishLaunchingwithoptions.

Am getting crashes in ios 8.1 at this line

shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

So i am returning if launchOptions == nil to fix the crash.

Below is the code am using in my app for using shortcut. Its created in my main app so I've modified names a little bit as an example.

How do i handle the shortcut which are not supported on early iOS ( from 8.0) or devices. I want my app to support both ios8 and greater versions.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Override point for customization after application launch.
  BOOL shouldPerformAdditionalDelegateHandling = YES;

  // Shortcut creation and methods
  [[MyAppShortcuts instance] createShortcutItem];

  if (launchOptions == nil) {
    return shouldPerformAdditionalDelegateHandling;
  }

  UIApplicationShortcutItem *shortcutItem;
  shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

  if ( shortcutItem != nil ){
    // Open app from shortcut
    self.rootViewCtrl_.shortcutItem_ = shortcutItem;

    [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
    [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];

    // This will block "performActionForShortcutItem:completionHandler" from being called.
    shouldPerformAdditionalDelegateHandling = NO;
  }
  return shouldPerformAdditionalDelegateHandling;
}


- (void) createShortcutItem {
  UIApplicationShortcutIcon* firstIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcutFirstItem"];
  UIApplicationShortcutItem* firstItem;
  firstItem = [[UIApplicationShortcutItem alloc]initWithType: firstItemType
                                                 localizedTitle: NSLocalizedString(@"First Item", nil)
                                              localizedSubtitle: nil
                                                           icon: firstIcon
                                                       userInfo: nil];
  //..... creating 2nd 3rd 4th item
  [UIApplication sharedApplication].shortcutItems = @[firstItem, secondItem, thirdItem, fourthItem];
}

application:performActionForShortcutItem: method is called everytime when the app is opened using shortcut. If i create shortcutItems(icon, title and type) inside the method it calls everytime, does that affect opening shortcut in any way because creating items again and again?

- (void) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    // Shortcut creation and methods
    [ [ MyAppShortcuts instance ] createShortcutItem ];
    [[FinAppShortcuts instance] handleShortCut:shortcutItem ];     
}
1

There are 1 best solutions below

3
A C On

You should place all logic regarding shortcut items after checking if it's available (iOS 9.1 onwards). I don't think launchOptions is nil for instances where it's not invoked by tapping the shortcut.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    BOOL shouldPerformAdditionalDelegateHandling = YES;

    //checks if you could user shortcut items. only available in iOS 9.1 onwards
    if ([UIApplicationShortcutItem class]){

        // Shortcut creation and methods
        [[MyAppShortcuts instance] createShortcutItem];

        if (launchOptions[UIApplicationLaunchOptionsShortcutItemKey]){
            // Open app from shortcut
            self.rootViewCtrl_.shortcutItem_ = shortcutItem;
            [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
            [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];
            // This will block "performActionForShortcutItem:completionHandler" from being called.
            shouldPerformAdditionalDelegateHandling = NO;
        }

    }
    return shouldPerformAdditionalDelegateHandling;
}