25

我所有的应用程序目前都是用 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];

    }
   }

这允许它工作,无需放入任何其他方法,或向 didFinishLaunchingWithOptions 添加任何内容。

4

5 回答 5

25

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

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

  • 一种是应用程序是否被终止或未在后台运行,我们在启动时获取快捷方式信息。

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

要在后台处理这些快速操作快捷方式,您需要在 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];

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

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

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

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

于 2015-09-24T23:18:39.810 回答
3

如果您查看为 Apple 提供的示例代码,您会发现他们建议您编写一个处理快捷方式项的方法,以便您可以在所有三个地方处理它:

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

然后,您只需在获得快捷方式项的任何地方调用此方法。这应该可以帮助您一路走好!

于 2015-09-28T19:54:45.400 回答
3

实施以下3个简单步骤:

第 1 步:在类中编写以下方法AppDelegate来配置动态快捷方式项。

注意:如果您希望它是静态的,您可以在 info.plist 中配置快捷方式项目。(请参阅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;
}
于 2017-06-30T06:37:57.950 回答
1

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

3D touch home 快速动作演示:https://github.com/dakeshi/3D_Touch_HomeQuickAction

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

如苹果文档中所述,您可以在application:didFinishLaunchingWithOptions:方法中处理快速操作。在这种情况下,您应该返回 NO 以阻止调用application:performActionForShortcutItem:completionHandler:方法。

于 2015-12-17T07:42:40.913 回答
0

它适用于 swift 3 和 4(仅在主屏幕快捷方式上)

//Add plist items as show in image and write following method in Appdelegate
//3D Touch Method shortcuts from home screen

func application(_ application: UIApplication, performActionFor shortcutItem:UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

if shortcutItem.type == "Share" {
    //handle action Share
    let alert = UIAlertController(title: "3D touch Share", message: "Yahoo!!! 3D touch is working", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.window?.rootViewController?.present(alert,animated: true,completion: nil)
    completionHandler(true)
   } else if shortcutItem.type == "Logout" {

    //handle action Type02
    let alert = UIAlertController(title: "3D touch Logout", message: "Yahoo!!! 3D touch is working", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.window?.rootViewController?.present(alert,animated: true,completion: nil)
    completionHandler(true)
   } else {
    completionHandler(false)
  }

 }

在此处输入图像描述

于 2018-02-16T05:39:02.597 回答