Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Touch Home Shortcuts In Obj-C

Tags:

All of my apps are currently written in Obj-C. The link https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545 for the sample code of implementing Home Screen Shortcuts with 3D Touch is completely compiled in Swift. Anyone come across documentation for Obj-C, so I don't have to go through my AppDelegate and translate it all?

UPDATE:

After adding in all the shortcuts in Info.plist, I added in the AppDelegate.m:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {     UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;      NSLog(@"%@", shortcutItem.type);     if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addPrayerRequest"]) {         Requests *gonow = [[Requests alloc] init];          [nav pushViewController:gonow animated:YES];      }     if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addPrayer"]) {        PrayerStats *controller = [[PrayerStats alloc] init];         [nav pushViewController:controller animated:YES];      }      if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addFast"]) {        FastStats *controller1 = [[FastStats alloc] init];         [nav pushViewController:controller1 animated:YES];      }      if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addStudy"]) {        StudyStats *controller2 = [[StudyStats alloc] init];         [nav pushViewController:controller2 animated:YES];      }    } 

This allows it to work, without putting any other methods in, or adding anything to didFinishLaunchingWithOptions.

like image 765
user717452 Avatar asked Sep 17 '15 15:09

user717452


People also ask

How to add action add to Home screen Iphone?

You can add shortcuts to the Home Screen, and optionally group them into folders. In My Shortcuts in the Shortcuts app on your iOS or iPadOS device, tap on a shortcut, then tap to open Details. Tap Add to Home Screen.

What is a quick action Apple shortcuts?

To allow a shortcut to run from within other apps, you need to set the shortcut as a quick action. In the Shortcuts app on your Mac, double-click a shortcut, then click . Click Details, then select Use as Quick Action. A new action appears at the beginning of your workflow defining what input the shortcut will accept.

What are quick actions iOS?

Quick actions are a great way to provide your users fast access to your app's common functionality within the home screen. iOS 13 introduced the concept of quick actions, where a user can touch and hold an app icon to display a set of shortcuts or actions to perform right from the home screen.


Video Answer


2 Answers

There are two states from where the user can open the app through Quick Actions.

TL;DR You are always doing the same thing regardless of the state in which the app is when the quick action is done, that's why you only need to override application:performActionForShortcutItem:completionHandler: So if you wanted to do different things, then you would want to handle them in the two places, if not then just the overridden is enough.

  • One is if the app is killed or not running in background where we get the shortcut info on launch.

  • The other is if the app is running in background where we get the shortcut info on the new app delegate method.

To handle these Quick Action shortcuts when in background you need to override this method on App Delegate:

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

And for not running in background (killed) on your

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

or

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

you should check if the app was launched by a Quick Action:

UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey]; 

(Link to related Apple Documentation) Quote from the Official Apple Docs

It’s your responsibility to ensure the system calls this method conditionally, depending on whether or not one of your app launch methods (application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions:) has already handled a quick action invocation. The system calls a launch method (before calling this method) when a user selects a quick action for your app and your app launches instead of activating.

The requested quick action might employ code paths different than those used otherwise when your app launches. For example, say your app normally launches to display view A, but your app was launched in response to a quick action that needs view B. To handle such cases, check, on launch, whether your app is being launched via a quick action. Perform this check in your application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method by checking for the UIApplicationLaunchOptionsShortcutItemKey launch option key. The UIApplicationShortcutItem object is available as the value of the launch option key.

If you find that your app was indeed launched using a quick action, perform the requested quick action within the launch method and return a value of NO from that method. When you return a value of NO, the system does not call the application:performActionForShortcutItem:completionHandler: method.

like image 60
Nicolas S Avatar answered Oct 06 '22 19:10

Nicolas S


If you look at the sample code provided for apple, you'll see that they suggest that you write a method that handles your shortcut item so that you can handle it in all three places:

  • application: performActionForShortcutItem,
  • application: didFinishLaunchingWithOptions and
  • willFinishLaunchingWithOptions

An example of what I did was:

- (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {     BOOL handled = NO;      if (shortcutItem == nil) {         return handled;     }      if ([shortcutItem.type isEqualToString:kFavoritesQuickAction]) {         handled = YES;     }       if (handled) {         // do action here     }      return handled; } 

Then you would just call this method in any place where you are getting a shortcut item. This should help you along your way!

like image 29
sdoowhsoj Avatar answered Oct 06 '22 19:10

sdoowhsoj