Obj-C中的3D Touch Home快捷键 [英] 3D Touch Home Shortcuts In Obj-C

查看:231
本文介绍了Obj-C中的3D Touch Home快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

更新:

在添加Info.plist中的所有快捷方式后,我在AppDelegate.m中添加了:

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];

    }
   }

这使它可以工作,没有将任何其他方法放入或添加任何东西到didFinishLaunchingWithOptions。

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

推荐答案

用户可以通过两种状态打开应用程序快速行动。

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

TL; DR
无论应用程序处于快速操作状态的状态如何,您始终都在做同样的事情。完成,这就是为什么你只需要覆盖应用程序:performActionForShortcutItem:completionHandler:所以如果你想做不同的事情,那么你会想要在两个地方处理它们,如果然后只是被覆盖就足够了。

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.

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

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

并且没有在你的

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

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

您应该检查该应用是否是由快速操作启动的:

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

UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];

(链接到相关的Apple文档)
来自Apple官方文档的引用

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


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

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.

请求的快速操作可能会使用不同于$ b的代码路径$ b那些用于你的时候应用程序启动。例如,假设您的应用
通常会启动以显示视图A,但是您的应用是在
中启动的,以响应需要查看B的快速操作。要处理此类情况,请在启动时进行
检查,您的应用程序是否通过快速
操作启动。通过检查
UIApplicationLaunchOptionsShortcutItemKey启动选项密钥,在
应用程序中执行此检查:willFinishLaunchingWithOptions:或
应用程序:didFinishLaunchingWithOptions:方法。
UIApplicationShortcutItem对象可用作
启动选项键的值。

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.

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

这篇关于Obj-C中的3D Touch Home快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆