IOS development – 3D touch application series I – quick actions create desktop icon shortcut

Time:2020-2-10

A word

I haven’t written essays for a long time. It’s more than a year. During the period, I also wanted to continue to write essays, but it was delayed for various reasons. Recently I thought about it again. There are still many things I want to write, share and record. After that, I will continue to write essays, but not only in the direction of IOS, but also to experience something new. Let’s start reading the text.

brief introduction

On devices that support 3D touch, quick actions enables users to complete the most frequently done tasks faster and with fewer steps, so many of which can be done directly through the main screen. For example, pressing the wechat icon hard and long will pop up common functions such as “scan” and “my QR code“, which I often use when I add friends, either “scan” others or “scan” others. Then this is its official introduction, 3D touch – IOS – Apple Developer.

The demo of this article has been put on GitHub. Please go to Garvey Calvin for demo/iOS-Travel Download.

The supported devices are iPhone 6S and 6S plus and subsequent iPhone series. The system supports 3D touch from IOS 9.0.

Info.plist create shortcut

Shortcuts can be defined directly in info.plist. The primary key isUIApplicationShortcutItems, which is an arrayArray。 Each key value can be viewed in the information property list key reference, and the function of each key is briefly introduced here.

Key Effect
Uiapplicationshortcutitemtype (required) Unique identification
Uiapplicationshortcutitemtitle (required) Title displayed
Uiapplicationshortcutitemsubtitle (optional) Displayed subtitles
Uiapplicationshortcutitemicontype (optional) Use system icons
Uiapplicationshortcutitemiconfile (optional) Use project’s icon
Uiapplicationshortcutitemuserinfo (optional) Additional information

The following is a detailed example. Except for uiapplicationshortcutitemuserinfo, all the above keys are implemented.

IOS development - 3D touch application series I - quick actions create desktop icon shortcut

Now let’s take a look at the operation effect. It is worth mentioning that when the icons are in the upper half screen and the lower half screen respectively, the display order will be different. The specific operation effect is as follows.

Icon on top half

IOS development - 3D touch application series I - quick actions create desktop icon shortcut

Icon in bottom half

IOS development - 3D touch application series I - quick actions create desktop icon shortcut

Determine which shortcut the user clicked

Implement the proxy method of uiapplicationdelegate in appdelegate. M fileapplication:performActionForShortcutItem:completionHandler:, you can know from which shortcut the user came in. The specific code is as follows.

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler  API_AVAILABLE(ios(9.0)) {
    NSLog(@"%s", __FUNCTION__);
    
    if ([shortcutItem.type isEqualToString:@"search"]) {
        Nslog (@ "user entered from shortcut" search ");
    } else if ([shortcutItem.type isEqualToString:@"list"]) {
        Nslog (@ "user entered from the shortcut" list ");
    } else if ([shortcutItem.type isEqualToString:@"public"]) {
        Nslog (@ "user comes in from the shortcut" one click publishing ");
    }
}

Code creation shortcuts

Shortcuts also support code creation. Each uiapplicationshortcutitem is a shortcut, which is generally implemented in the appdelegate. M fileapplication:didFinishLaunchingWithOptions:Write the implementation code in. The code is easy to understand. I will not explain it here.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%s", __FUNCTION__);
    
    if (@available(iOS 9.0, *)) {
        UIApplicationShortcutIcon *searchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
        Uiapplicationshortcutitem * search = [[uiapplicationshortcutitem alloc] initwithtype: @ "search" localizedtitle: @ "search" localizedsubtitle: Nil Icon: searchicon userinfo: Nil];
        
        UIApplicationShortcutIcon *publicIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"upload"];
        Uiapplicationshortcutitem * public = [[uiapplicationshortcutitem alloc] initwithtype: @ "public" localizedtitle: @ "one click publishing" localizedsubtitle: Nil Icon: publicicon userinfo: Nil];
        
        Uiapplicationshortcutitem * list = [[uiapplicationshortcutitem alloc] initwithtype: @ "list" localizedtitle: @ "list" localizedsubtitle: @ "regional ranking" icon: Nil userinfo: Nil];
        
        application.shortcutItems = @[list, public, search];
    }
    
    return YES;
}

data

The demo of this article has been put on GitHub. Please go to Garvey Calvin for demo/iOS-Travel Download.

Environmental Science

Xcode: Version 10.1 (10B61)

Test model: iPhone XS Max

Test system: IOS 12.1

summary

It’s easy to create quick actions in info.plist or code. There’s no complicated logic and it’s easy to learn.

The use of Quick Actions is quite different. As far as I am concerned, I often use Alipay and WeChat’s “sweep away”, Alipay’s “riding code” and so on.

3D touch can be used not only in these fields, but also in peek and pop and pressure sensitivity. This series of articles may be added in the future.

statement

By Garvey Calvin
Source: http://www.cnblogs.com/garveycalvin/
The copyright of this article belongs to the author and the blog park. You are welcome to reprint, but you must keep this statement and give the original link. Thank you for your cooperation!