The instant navigation
Original address: medium.com/ivymobility…
The original author: ping2karthikeyan.medium.com/
Published: March 28, 2020
Quick action is the most convenient way to do useful or any application-specific action on the home screen.
In this article, I explain quick action, not gadgets.
Quick Action is available on all devices running iOS 13 or later.
When you long press or forcibly touch the app icon (if any), a list of available quick actions is displayed.
The position of the list depends on where the app is on the home screen.
Each quick action has a title, subtitle (optional), and image icon (optional).
Each application can provide up to four quick actions. If there are more than four, the first four quick actions are visible only to the user. Each application has its default system quick actions, such as “edit home screen”, “share application”, and “Delete Application”.
Here, headings and subheadings are always left-aligned for left-to-right languages, and vice versa.
Apple’s guidelines for implementing rapid action.
- Create quick actions for useful and creative tasks.
- Avoid fast action for operations that involve complex navigation to the target screen.
- If fast action is configured dynamically, make sure it doesn’t cause any unpredictable changes.
- Each quick action title should be short and clear.
- Do not use quick action for notifications.
- Provide a recognizable icon for each quick action. You can use either system ICONS or custom ICONS.
- Limit the use of emoji because it’s not monochrome and it doesn’t align properly.
Fast action can be configured as static or dynamic.
- Static actions at build time are visible when the user installs or updates the application. Also, if the quick actions that are appropriate for your application never change, define them as static quick actions in your project’s info.plist file.
- Dynamic actions at runtime are visible to the user after the application has been started at least once. At this point, you can configure these actions based on the user’s behavior.
Define static quick actions
Quick action can be in the application’s Info. In the file to add static UIApplicationShortcutItems array. Each element of the array is a UIApplicationShortcutItem dictionary, on behalf of the quick action. It contains the following entries
- UIApplicationShortcutItemTitle. A necessary string to display as the title of your action. If necessary, it will be displayed as a maximum of two lines. In addition, we can localize the title if necessary.
- UIApplicationShortcutItemSubtitle. An optional string that is displayed as the subtitle of your action and is displayed under the title. It will be displayed as a single line. In addition, we can localize the subtitle if necessary.
- UIApplicationShortcutItemIconType. An optional string of image ICONS that describes which home screen quick action icon should be used for the shortcut.
- UIApplicationShortcutItemIconFile: an optional string, for custom image icon name. The supplied image names will be loaded from the application’s bundle and will be masked to conform to the system-defined icon style. Apple recommends that custom image ICONS be square, monochrome, 35×35 points.
- UIApplicationShortcutItemUserInfo. An optional application-specific piece of information that can be provided to the application to perform quick operations.
- UIApplicationShortcutItemType. A necessary unique string that is passed to your application when the user clicks quick action. It is used to identify the type of quick operation to perform.
A snapshot of the info.plist file shows how to statically add quick actions.
If you find typing slow, copy the following, open info.plist as source code and paste it.
<key>UIApplicationShortcutItems</key>\
<array>\
<dict>\
<key>UIApplicationShortcutItemTitle</key>\
<string>Search</string>\
<key>UIApplicationShortcutItemSubtitle</key>\
<string>by product name</string>\
<key>UIApplicationShortcutItemIconType</key>\
<string>UIApplicationShortcutIconTypeSearch</string>\
<key>UIApplicationShortcutItemType</key>\
<string>QuickAction.Search</string>\
</dict>\
<dict>\
<key>UIApplicationShortcutItemTitle</key>\
<string>Saved Items</string>\
<key>UIApplicationShortcutItemIconFile</key>\
<string>SavedItems</string>\
<key>UIApplicationShortcutItemType</key>\
<string>QuickAction.SavedItems</string>\
</dict>\
<dict>\
<key>UIApplicationShortcutItemTitle</key>\
<string>Cart</string>\
<key>UIApplicationShortcutItemIconFile</key>\
<string>cart.png</string>\
<key>UIApplicationShortcutItemType</key>\
<string>QuickAction.Cart</string>\
</dict>\
</array>
Copy the code
Define dynamic fast action
It can be Shared by the UIApplication instance shortcutItems attribute is set to UIApplicationShortcutItem array of objects to be configured. It looks like a UIApplication. Shared. ShortcutItems = [shortcutItem].
func addSearchQuickAction(a) {
removeSavedItemsQuickAction()
let searchItem = UIApplicationShortcutItem(type: ApplicationShortcutItemType.search.rawValue,
localizedTitle: ApplicationShortcutItemTitle.search.rawValue,
localizedSubtitle: ApplicationShortcutItemSubTitle.search.rawValue,
icon: UIApplicationShortcutIcon(type: .update),
userInfo: nil)
var shortcutItems = UIApplication.shared.shortcutItems ?? []
shortcutItems.append(searchItem)
UIApplication.shared.shortcutItems = shortcutItems
}
Copy the code
Add quick actions dynamically
func removeSearchQuickAction(a) {
var shortcutItems = UIApplication.shared.shortcutItems ?? []
for (index, item) in shortcutItems.enumerated() {
if item.type = = ApplicationShortcutItemType.search.rawValue {
shortcutItems.remove(at: index)
}
}
UIApplication.shared.shortcutItems = shortcutItems
}
Copy the code
Dynamic delete quick action
In the example above, the quickAction (if any) was removed before the quickAction was added to avoid redundancy.
Deal with quick action
When the user triggers a home screen quick action, your application is notified in one of the following ways.
- If the application is not in the running state, so fast the details of the project will be through the method application (_ : didFinishLaunchingWithOptions launchOptions parameters:).
- If the application is running, the following method application in the AppDelegate (_ : performActionFor: completionHandler:) will be triggered.
var launchedShortcutItem: UIApplicationShortcutItem?
func application(_ application: UIApplication.didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// If the app is launched by Quick Action, then take the relevant action
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
launchedShortcutItem = shortcutItem
// Since, the app launch is triggered by QuicAction, block "performActionForShortcutItem:completionHandler" method from being called.
return false
}
return true
}
Copy the code
Handle fast action if the application is not loaded
func application(_ application: UIApplication.performActionFor shortcutItem: UIApplicationShortcutItem.completionHandler: @escaping (Bool) - >Void) {
guard let actionType = ApplicationShortcutItemType(rawValue: shortcutItem.type) else { return }
switch actionType {
case .search:
print("Handle search action")
case .savedItems:
print("Handle Saved Items action")
case .cart:
print("Handle Cart action")}}Copy the code
Handle fast action if the application has been loaded
I don’t always use direct string comparisons. So Enum types are used for comparison.
enum ApplicationShortcutItemType:String {
case search = "QuickAction.Search"
case savedItems = "QuickAction.SavedItems"
case cart = "QuickAction.Cart"
}
enum ApplicationShortcutItemTitle:String {
case search = "Search"
case savedItems = "Saved Items"
case cart = "Cart"
}
enum ApplicationShortcutItemSubTitle:String {
case search = "by product name"
}
Copy the code
The Enum type used to handle strings
The final output
Facts about fast action
- If the application has just been installed, only static quick actions (if available) are shown when you press the application icon on the home screen.
- After the application is first launched, dynamic actions (if available) are displayed when the application icon is pressed on the home screen.
- If the application is updated, the old quick action is only visible to the user (if available) until the updated application is launched.
- However, the above situation can reportedly be resolved by providing the application version in the quick-action userInfo parameter.
- If fast actions are static and dynamically configured, then dynamic actions are given high priority. If there is room for it, static quick action is added last.
Get the complete source code at GitHub
Karthikeyan-Thiru/QuickAction
Contribute to karthikeyan-Thiru /QuickAction development by creating an account on GitHub.
github.com
Resources. Apple document
You think it reads good?
Recommend this article (by clicking the 👏 button) and make it available to others at…… Contact me Karthikeyan on LinkedIn
Karthikeyan ThirugnanaSambandam – software engineer – Ivy Mobility | LinkedIn
Experienced iOS developer with good working experience in the consumer services industry. Master STH.
www.linkedin.com
www.deepl.com translation