Obj-C中的3D Touch Home快捷键

use*_*452 24 objective-c ios swift ios9 3dtouch

我的所有应用程序目前都是用Obj-C编写的.有关使用3D Touch实现主屏幕快捷方式的示例代码的链接https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545已完全编译在斯威夫特.任何人都会遇到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];

    }
   }
Run Code Online (Sandbox Code Playgroud)

这允许它工作,而不需要添加任何其他方法,或者向didFinishLaunchingWithOptions添加任何内容.

Nic*_*s S 25

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

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

  • 一个是如果应用程序被杀死或未在后台运行,我们在启动时获取快捷方式信息.

  • 另一个是如果应用程序在后台运行,我们会在新的app delegate方法中获取快捷方式信息.

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

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
Run Code Online (Sandbox Code Playgroud)

而不是在你的背景中运行(杀死)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)

要么

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)

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

UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
Run Code Online (Sandbox Code Playgroud)

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

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

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

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