在Obj-C中的3D触摸主页快捷方式

我所有的应用程序目前都是用Obj-C编写的。 链接https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545为实现主屏幕快捷方式与3D Touch的示例代码完全编译在Swift中。 任何人都会遇到Obj-C的文档,所以我不需要通过我的AppDelegate并翻译它呢?

更新:

在Info.plist中添加所有快捷方式后,我在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]; } } 

这可以使其工作,而不需要添加任何其他方法,或者添加任何内容到didFinishLaunchingWithOptions。

有两种状态,用户可以通过快速操作打开应用程序。

TL; DR当快速操作完成时,无论应用程序的状态如何,您总是在做同样的事情,这就是为什么您只需要重写application:performActionForShortcutItem:completionHandler:因此,如果您想做不同的事情,那么您想要在两个地方处理他​​们,如果不是那么只是被覆盖就够了。

  • 一个是如果该应用程序被杀死或没有在后台运行,我们得到启动快捷方式的信息。

  • 另一个是如果应用程序在后台运行,我们得到新的应用程序委托方法的快捷方式信息。

要在后台处理这些Quick Action快捷方式,您需要在App Delegate上覆盖此方法:

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

而不是在后台运行(杀死)你的

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

要么

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

您应该检查应用程序是否通过快速操作启动:

 UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey]; 

(链接到相关的苹果文件)从官方的苹果文件引用

您有责任确保系统有条件地调用此方法,具体取决于您的某个应用启动方法(application:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions :)是否已经处理了快速操作调用。 系统调用启动方法(在调用此方法之前),当用户为您的应用程序select一个快速操作,并启动您的应用程序而不是激活。

请求的快速操作可能会使用与您的应用程序启动时使用的代码path不同的代码path。 例如,假设您的应用程序正常启动以显示视图A,但是您的应用程序是为了响应需要视图B的快速操作而启动的。要处理此类情况,请在启动时检查您的应用程序是否正在通过快速操作启动。 在应用程序中执行此检查:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:方法,方法是检查UIApplicationLaunchOptionsShortcutItemKey启动选项密钥。 UIApplicationShortcutItem对象可用作启动选项密钥的值。

如果您发现应用程序确实是使用快速操作启动的,请在启动方法中执行请求的快速操作,并从该方法返回NO值。 当您返回NO值时,系统不会调用应用程序:performActionForShortcutItem:completionHandler:方法。

如果你看看为苹果提供的示例代码,你会发现他们build议你编写一个处理你的快捷方式的方法,这样你就可以在三个地方处理它:

  • application: performActionForShortcutItem
  • application: didFinishLaunchingWithOptions
  • willFinishLaunchingWithOptions

我做的一个例子是:

 - (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; } 

那么你可以在任何你正在获取快捷方式的地方调用这个方法。 这应该帮助你一路顺风!

执行以下3个简单的步骤:

第一步:AppDelegate类下面编写方法configurationdynamic快捷方式项。

注意:如果您希望静态,您可以在info.plist中configuration快捷方式。 (请参阅Apple文档。 )

 /** * @brief config dynamic shortcutItems * @discussion after first launch, users can see dynamic shortcutItems */ - (void)configDynamicShortcutItems { // config image shortcut items // if you want to use custom image in app bundles, use iconWithTemplateImageName method UIApplicationShortcutIcon *shortcutSearchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite]; UIApplicationShortcutItem *shortcutSearch = [[UIApplicationShortcutItem alloc] initWithType:@"com.sarangbang.QuickAction.Search" localizedTitle:@"Search" localizedSubtitle:nil icon:shortcutSearchIcon userInfo:nil]; UIApplicationShortcutItem *shortcutFavorite = [[UIApplicationShortcutItem alloc] initWithType:@"com.sarangbang.QuickAction.Favorite" localizedTitle:@"Favorite" localizedSubtitle:nil icon:shortcutFavoriteIcon userInfo:nil]; // add all items to an array NSArray *items = @[shortcutSearch, shortcutFavorite]; // add the array to our app [UIApplication sharedApplication].shortcutItems = items; } 

第2步:AppDelegateapplication didFinishLaunchingWithOptions方法写下面的代码。

  // UIApplicationShortcutItem is available in iOS 9 or later. if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){ [self configDynamicShortcutItems]; // If a shortcut was launched, display its information and take the appropriate action UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey]; if(shortcutItem) { // When the app launch at first time, this block can not called. //App launch process with quick actions [self handleShortCutItem:shortcutItem]; }else{ // normal app launch process without quick action } } 

第3步:AppDelegate类中写下委托方法和完成处理程序。

 /* Called when the user activates your application by selecting a shortcut on the home screen, except when application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`. You should handle the shortcut in those callbacks and return `false` if possible. In that case, this callback is used if your application is already launched in the background. */ - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{ BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem]; completionHandler(handledShortCutItem); } /** * @brief handle shortcut item depend on its type * * @param shortcutItem shortcutItem selected shortcut item with quick action. * * @return return BOOL description */ - (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{ BOOL handled = NO; NSString *bundleId = [NSBundle mainBundle].bundleIdentifier; NSString *shortcutSearch = [NSString stringWithFormat:@"%@.Search", bundleId]; NSString *shortcutFavorite = [NSString stringWithFormat:@"%@.Favorite", bundleId]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; if ([shortcutItem.type isEqualToString:shortcutSearch]) { handled = YES; SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"secondVC"]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; } else if ([shortcutItem.type isEqualToString:shortcutFavorite]) { handled = YES; ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"thirdVC"]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; } return handled; } 

我为主屏幕快速操作制作了一个Objective-C演示项目。

3D触摸主页快速操作演示:https://github.com/dakeshi/3D_Touch_HomeQuickAction

Demo项目在没有Info.plist文件的情况下执行静态快速操作,以避免在启动应用程序之前出现不必要的情况。 您可以轻松将其更改为dynamic快速操作。

正如苹果文档中提到的,您可以在应用程序中处理快速操作:didFinishLaunchingWithOptions:方法。 在这种情况下,您应该返回NO来阻止调用应用程序:performActionForShortcutItem:completionHandler:方法。