3D Touch Home Shortcuts In Obj-C

前端 未结 5 900
一整个雨季
一整个雨季 2021-01-30 05:37

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/

5条回答
  •  情书的邮戳
    2021-01-30 06:21

    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.)

    /**
     *  @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;
    }
    

    Step 2 : In AppDelegate class application didFinishLaunchingWithOptions method write below code.

        // 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
                }
    
            }
    

    Step 3 : Write below delegate method and completion handler in AppDelegate class.

    /*
     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;
    }
    

提交回复
热议问题