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.
Implement below 3 simple steps:
Step 1 : Write below method in
AppDelegate
class to Configure dynamic shortcut items.NOTE : You can configure shortcut items in info.plist if you want it static. (Refer Apple documentation.)
Step 2 : In
AppDelegate
classapplication didFinishLaunchingWithOptions
method write below code.Step 3 : Write below delegate method and completion handler in
AppDelegate
class.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
andwillFinishLaunchingWithOptions
An example of what I did was:
Then you would just call this method in any place where you are getting a shortcut item. This should help you along your way!
It works on both swift 3 and 4 (only on home screen shortcuts)
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:
And for not running in background (killed) on your
or
you should check if the app was launched by a Quick Action:
(Link to related Apple Documentation) Quote from the Official Apple Docs
I make an objective-c demo project for home screen quick action.
3D touch home quick action demo : https://github.com/dakeshi/3D_Touch_HomeQuickAction
The Demo project implements the static quick action without Info.plist file to avoid unwanted situations before launching the app at first. You can easily change it to the dynamic quick action.
As mentioned in apple documentation, you can handle quick action in application:didFinishLaunchingWithOptions: method. In that case, you should return NO to block to call application:performActionForShortcutItem:completionHandler: method.