Search code examples
iosobjective-c3dtouch

Call IBAction in App Delegate


I want to add 3d touch home screen shortcuts to my objective c app. The main part of my app functions when a UIButton is pressed. This calls the method in the ViewController.h and ViewController.m

- (IBAction)StartScanning:(id)sender;

This allows the camera to start functioning and pushes the correct view controller and desired methods.

I have read many walkthroughs but still cannot understand how to start this IBAction when a 3d touch shortcut is pressed.

Sorry if this is a repeat question or I am just being stupid. I'm a bit new to all of this.


Solution

  • It's not that hard to do and as you mentioned, there's a lot of tutorials out there to help. In summary, first you need to add the 3d touch delegate to your ViewController.h or .m. <UIViewControllerPreviewingDelegate>

    That will give you access to the delegate methods you need to show the home screen shortcuts. Here's an example of one of my apps (name removed in this example).

    In my AppDelegate, performActionForShortcutItem:completionHandler: is called first and sent the shortcut the user selected. Use it to determine how to respond to the shortcut. I passed the shortcut to a method, handleShortcutItem:shortcutItem" that would determine which storyboard I would use (I know there's no 3D Touch in iPads right now but I wanted to build in the code for when Apple comes out with one).

    Based on the shortcut, I create my ViewController and pass the shortcut to the method logShortcutUsed, passing in the shortcut title.

    #pragma mark - Shortcut Items
    
    - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    completionHandler([self handleShortcutItem:shortcutItem]);
    }
    
    - (BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItem {
    UIStoryboard *storyboard;
    UINavigationController *navController = (UINavigationController *) self.window.rootViewController;
    if (IS_IPAD()) {
        storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
    } else {
        storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    }
    
    xxxViewController *vb = (xxxViewController *)navController.topViewController;
    
    if ([shortcutItem.localizedTitle isEqualToString:@"New Match"]) {
        [vb logShortcutUsed:shortcutItem.localizedTitle];
        [vb startNewMatch];
        return TRUE;
    
    } else if ([shortcutItem.localizedTitle isEqualToString:@"New Game"]) {
        [vb logShortcutUsed:shortcutItem.localizedTitle];
        [vb gamePressedFromShortcut];
        return TRUE;
    
    }
    
    return FALSE;
    }
    

    In my main ViewController, I create the dynamic shortcuts (you can have static or dynamic shortcuts). This is what will be seen by the user when they 3D Touch the icon. I include an icon as well, that's optional. The shortcutItems is just an array of UIApplicationShortcutItems.

    - (void)setupDynamicShortcuts {
    UIApplicationShortcutItem *newMatch = [[UIApplicationShortcutItem alloc] initWithType:@"$(PRODUCT_BUNDLE_IDENTIFIER).NewMatch"
                                                                           localizedTitle:NSLocalizedString(@"New Match", @"Start a new match")
                                                                        localizedSubtitle:NSLocalizedString(@"Start a new match", @"Start a new match button.")
                                                                                     icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"Sport Net-50"]
                                                                                 userInfo:nil];
    
    UIApplicationShortcutItem *newGame = [[UIApplicationShortcutItem alloc] initWithType:@"$(PRODUCT_BUNDLE_IDENTIFIER).NewGame"
                                                                           localizedTitle:NSLocalizedString(@"New Game", @"Start a new game")
                                                                        localizedSubtitle:NSLocalizedString(@"Start a new game", @"Start a new game button.")
                                                                                    icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"volleyball-50"]
                                                                                 userInfo:nil];
    
    [UIApplication sharedApplication].shortcutItems = @[newMatch, newGame];
    
    }
    

    In the same ViewController are the methods that will be called from the AppDelegate, startNewMatch and gamePressedFromShortcut. I also log these calls to my analytics so I can track how many times people use this feature, which is something I strongly suggest.

    It's not as difficult as it initially seems.