iOS

Add Home Screen Quick Actions in SwiftUI

On the Home screen of a device running iOS 13 or later, apps can display Home Screen quick actions when users touch and hold the app icon (on a 3D Touch device, users press briefly on the icon). many apps are coming with...

J
Joynal Abedin
6
\"\"

On the Home screen of a device running iOS 13 or later, apps can display Home Screen quick actions when users touch and hold the app icon (on a 3D Touch device, users press briefly on the icon). many apps are coming with this Quick Actions. In this post, we are going to learn how can we implement the “Home Screen Quick Actions” in SwiftUI

There are two types of Quick Actions:

  1. Static
  2. Dynamic

Static:

Static actions are never changing actions. It is defined in the project’s Info.plist file. If the quick actions appropriate for an app never change, define them as static quick actions using the project’s Info.plist file. This sample project defines two static quick actions: one for searching and one for sharing. The UIApplicationShortcutItems key in the Info.plist file contains an array of two dictionaries that represent the two static quick actions.

Step_01: Click Xcode then choose Create New Project...

\"\"

Create a project file in iOS development target & add this array of dictionary keys in info.plist file.

<key>UIApplicationShortcutItemskey>
<array>
    <dict>
        <key>UIApplicationShortcutItemTypekey>
        <string>SearchActionstring>
        <key>UIApplicationShortcutItemIconTypekey>
        <string>UIApplicationShortcutIconTypeSearchstring>
        <key>UIApplicationShortcutItemTitlekey>
        <string>Searchstring>
        <key>UIApplicationShortcutItemSubtitlekey>
        <string>Search for an itemstring>
    dict>
    <dict>
        <key>UIApplicationShortcutItemTypekey>
        <string>ShareActionstring>
        <key>UIApplicationShortcutItemIconTypekey>
        <string>UIApplicationShortcutIconTypeSharestring>
        <key>UIApplicationShortcutItemTitlekey>
        <string>Sharestring>
        <key>UIApplicationShortcutItemSubtitlekey>
        <string>Share an itemstring>
    dict>
array>

As you can see UIApplicationShortcutItems is used to add the static quick shortcut buttons. This is an array of a dictionary. In each dictionary UIApplicationShortcutItemType and UIApplicationShortcutItemTitle are mandatory keys.

  1. UIApplicationShortcutItemType: This unique string represent for the quick action.
  2. UIApplicationShortcutItemTitle: This title of the Quick Action button.

Now if your run this project & hold project icon then you will see this same as below:

\"\"

Dynamic:

Dynamic quick actions depend on specific data or state of the app. Dynamic quick actions are added to the app at the run time. these quick actions are an array of UIApplicationShortcutItem assigned to the shared instance of UIApplication.

Step_01: Write this code same as below in your project root directory i means the entry point of struct of the app.

import SwiftUI

@main
struct QuickActionsDemoApp: App {
    
    @Environment(\.scenePhase) var phase

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: phase) { (newPhase) in
            switch newPhase {
            case .active :
                print(\"App in active\")
            case .inactive:
                print(\"App is inactive\")
            case .background:
                print(\"App in Back ground\")
                addQuickActionsArray()
            @unknown default:
                print(\"default\")
            }
        }
    }
    
    func addQuickActionsArray() {
        UIApplication.shared.shortcutItems = [
            UIApplicationShortcutItem(type: \"History\", localizedTitle: \"History\"),
            UIApplicationShortcutItem(type: \"Chat\", localizedTitle: \"Chat\"),
            UIApplicationShortcutItem(type: \"DeleteItem\", localizedTitle: \"DeleteItem\"),
            UIApplicationShortcutItem(type: \"Contacts\", localizedTitle: \"Contacts\"),
        ]
    }
}

Here, we are creating four shortcuts for showing our App icon Quick actions:

1. History
2. Chat
3. DeleteItem
4. Contacts

We need to call this method when the app goes into the background state. So, we are using @Environment(\.scenePhase) environment variable and onChange(of:) modifier to capture the state of the app.

Now if your run this project & hold project icon then you will see this same as below:

\"\"

If you see then you will be understand that we added four quick actions but here added two quick actions. There why missing other two actions?. Actually, we can add any number of quick actions but Apple will show a maximum of four shortcuts only.

Continue...

J

Written by Joynal Abedin

Passionate about technology, code, and sharing knowledge.

0 Comments

Leave a Comment