Published on

Using Quick Actions and 3D Touch

Authors
  • avatar
    Name
    how-to.digital
    Twitter

Using Quick Actions and 3D Touch

Quick Actions and 3D Touch are powerful features available on iOS devices that allow users to quickly access app features and shortcuts. In this tutorial, we will explore how to implement and leverage Quick Actions and 3D Touch in your iOS app.

Prerequisites

To follow along with this tutorial, you will need:

  • A Mac computer running macOS Big Sur or later
  • Xcode 12 or later
  • An iOS device with 3D Touch support (iPhone 6s or newer)

1. Enabling 3D Touch

To begin, ensure that 3D Touch is enabled on your iOS device:

  1. Open the "Settings" app on your iOS device.
  2. Scroll down and tap on "Accessibility".
  3. Tap on "Touch" and then "Touch Accommodations".
  4. Toggle on "3D Touch" if it's not already enabled.

2. Configuring the App Icon for Quick Actions

Quick Actions are accessed through the app icon on the home screen. Follow these steps to configure the app icon for Quick Actions:

  1. Open your Xcode project.
  2. In the project navigator, select the target for your app.
  3. Click on the "Info" tab.
  4. Under the "App Icons and Launch Images" section, click on the disclosure triangle next to "App Icons Source".
  5. Locate the "Quick Actions" section and click on the disclosure triangle.
  6. Add the required primary and secondary quick actions for your app, specifying the title and icon for each.

3. Implementing Quick Actions

Now, let's implement the code to handle Quick Actions in your app:

  1. Open your Xcode project.
  2. Navigate to the file that contains your app's entry point (usually AppDelegate.swift).
  3. Inside the application(_:didFinishLaunchingWithOptions:) method, add the following code to check if the app was launched from a Quick Action:
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
    handleQuickAction(shortcutItem)
}
  1. Implement the handleQuickAction(_:) method to handle the Quick Action:
func handleQuickAction(_ shortcutItem: UIApplicationShortcutItem) {
    guard let shortcutType = shortcutItem.type.components(separatedBy: ".").last else {
        return
    }
    
    switch shortcutType {
        case "myapp.create":
            // Handle "Create" Quick Action
            // Example: Present a new view controller for creating a new item.
        case "myapp.search":
            // Handle "Search" Quick Action
            // Example: Present a search view controller.
        default:
            break
    }
}
  1. To handle Quick Actions when the app is already running, add the following code to the application(_:performActionFor:completionHandler:) method:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    handleQuickAction(shortcutItem)
    completionHandler(true)
}

4. Implementing 3D Touch Peek and Pop

In addition to Quick Actions, 3D Touch also provides Peek and Pop functionality, allowing users to preview and interact with content without fully opening it. To implement Peek and Pop in your app, follow these steps:

  1. In your view controller, conform to the UIViewControllerPreviewingDelegate protocol.
  2. Implement the registerForPreviewing(with:sourceView:) method to register for Peek and Pop:
if traitCollection.forceTouchCapability == .available {
    registerForPreviewing(with: self, sourceView: view)
}
  1. Implement the previewingContext(_:viewControllerForLocation:) method to provide a view controller for the Peek gesture:
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    let viewController = // Create and return the view controller that should be previewed
    return viewController
}
  1. Implement the previewingContext(_:commit:) method to handle the Pop gesture when the user fully presses the screen:
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    // Present the full view controller
}
  1. Optionally, you can adjust the Peek and Pop preview size by implementing the previewingContext(_:previewForHighlighting:) method:
func previewingContext(_ previewingContext: UIViewControllerPreviewing, previewForHighlighting viewController: UIViewController) -> UIView? {
    let previewView = // Create and return a custom preview view
    return previewView
}

Conclusion

Congratulations! You have learned how to enable and implement Quick Actions and 3D Touch in your iOS app. By providing shortcuts and Peek and Pop functionality, you can enhance the user experience and make your app more accessible and efficient. Explore further customization options and experiment with different features to create an immersive experience for your users.